Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push notification data not getting when app launched directly by clicking app icon

I have a scenario in which app will get push notification and need to show that messages in home screen of my app, for that i saved the message array into user defaults from my app delegate and everything works well, but in following conditions it's not working

  1. if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )

  2. if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also

Then i searched for solutions and came to know about silent push notifications (for background mode) and nothing else so i need to know how to handle all scenarios by push notifications and my appdelegete is

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];


    if (remoteNotif) {

        [self handlePushMessage:remoteNotif];
    }
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    [self handlePushMessage:userInfo];

}

-(void)handlePushMessage :(NSDictionary*)userInfo{

   //method to handle push message
}

Thanks in advance

like image 886
Anshad Rasheed Avatar asked Nov 02 '15 05:11

Anshad Rasheed


2 Answers

This is a common issue: if the user does not open your app by means of the displayed notification, there is no* way of getting the related information.

* A possible solution employed by many apps is to check with a remote server for unread notifications (e.g. check for an unset read-date field).

like image 189
tilo Avatar answered Oct 10 '22 06:10

tilo


In scenario 1. NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; here remoteNotif return nil as you enter into the app through triggering app icon.

In scenario 2. You can get push notification info through the following method

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
      if (userInfo) {

          [self handlePushMessage:userInfo];
      }
}
like image 29
Md. Reaz Murshed Khan Avatar answered Oct 10 '22 05:10

Md. Reaz Murshed Khan