0

How to fetch and display the message in push notification from dynamic notification service

push: {
    aps =  {
        alert = "My First Notification";
        sound = default;
        Msg = {
            myData = (
                      {
                        Msg = "Awesome";
                        Id = 123;
                        Date = "Jan 18 2018";
                       }
                      );
              };
           };
       }

As i want to display alert = "My First Notification" and Msg = "Awesome" when push notification arrives. I have no idea how to fetch and display. Please help me to solve this issue. TIA

I have tried below code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerForRemoteNotifications];


    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);

    NSString *deviceTokenStr = [[[[deviceToken description]
                                  stringByReplacingOccurrencesOfString: @"<" withString: @""]
                                 stringByReplacingOccurrencesOfString: @">" withString: @""]
                                stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"Device Token: %@", deviceTokenStr);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    NSLog(@"Received notification: %@", userInfo);
    NSUserDefaults * loginDefaults = [NSUserDefaults standardUserDefaults];

    UIApplicationState state = [[UIApplication sharedApplication] applicationState];
    if (UIApplicationStateActive == state )
    {

        return;
    }

}

I Want to display it in push notification only.

Rajeev
  • 99
  • 2
  • 12

2 Answers2

0

First of all UserInfo is an NSDictionary so you can get all your needed values by keys in your case will be something like this

code

NSDictionary * aps = [userInfo objectForKey:@"aps"];
NSDictionary * myData = [aps objectForKey:@"Msg"];
//inside of Msg dictionary you have an array of dictionaries
NSArray * arrayOfData = [myData objectForKey:@"myData"];
NSLog(@"%@",[arrayOfData objectAtIndex:0]);
NSDictionary * infoDict = [arrayOfData objectAtIndex:0];
//here we get the message!!!
NSString* message = [infoDict objectForKey:@"Msg"];
//then show your alert
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"My First Notification", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
[[[self window] rootViewController] presentViewController:alertController animated:true completion:nil];

This code was not tested let me know if something is wrong, but this is the idea

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • i think he is beginner so you can add `NSString *awesomeMsg=[[arrMyData objectAtIndex:0] valueForKey:@"Msg"]; // get your Msg - Awesome ` this line so he can get message also. – Nirav Kotecha Jan 18 '18 at 06:57
  • 1
    Check my answer again @NiravKotecha even its showing the alert now, let me know – Reinier Melian Jan 18 '18 at 06:59
  • Is the messages are displayed in push notification or in AlertView. i Want to display it in push notification only – Rajeev Jan 18 '18 at 06:59
  • @Rajeev I don't understand what you need, if you want display the message of the push notification in your app you need to do it with an UIAlertView – Reinier Melian Jan 18 '18 at 07:12
  • I don't want to display the message of push notification in AlertView. i want to display message in Push Notification – Rajeev Jan 18 '18 at 07:18
  • When push notification arrives, push notification should contain the data of My First Notification and Awesome – Rajeev Jan 18 '18 at 07:21
  • the push notification is showed by default when your app is closed or in background the push notification will be showed by default but with the message defined in the alert key in your aps dictionary, `alert = "My First Notification";` @Rajeev – Reinier Melian Jan 18 '18 at 08:05
0

As far as I understand from your comments you want your push to display title My First Notification and message (or body) Awesome on the notification screen. To do that you don't need to write the code in the app. It's the structure of the payload that does the magic.

Currently you're sending payload as

{
  "aps": {
    "alert": "My First Notification",
    "sound": "default",
    "Msg": {
      "myData": [
        {
          "Msg": "Awesome",
          "Id": 123,
          "Date": "Jan 18 2018"
        }
      ]
    }
  }
}

Which will not display Awesome message on the notification screen as per apple docs

To display Awesome message on the notification screen you need to specify body key. Create a payload like below and send this push, you'll see the title and body on the notification screen. Also go through the above docs for more informations

{
  "aps": {
    "alert": {
      "title": "My First Notification",
      "body": "Awesome",
      "sound": "default"
    }
  },
  "myData": {
    "Id": 123,
    "Date": "Jan 18 2018"
  }
}

At the end I would like to add that in android we need to write the code in the app to display the push notification whereas in ios we don't have to write the code or to be more precise our app doesn't control how the push will be displayed. It's all upto the server that is sending the data to APNS with appropriate keys

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184