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

4

9

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

Oskar answered 2/11, 2015 at 5:1 Comment(6)
If your app is terminated by the user then there is no way to find out about push notifications that are delivered if the user launches your app from the app iconCline
i think so , its better u create web services which gives u response of all messages sent till now.bcz the first scenario cannot be handled,The second can be handled thru silent notification.Chainplate
thanks for the quick response :) if notification came before user launched the app first time then what will happen ?Oskar
okei is there any chance for appstore rejection in the case of silent notificationOskar
You cannot receive remote notifications before your user has launched your app for the first time because you haven't yet registered for remote notifications and your server doesn't have the remote notification identifier in order to send a notification to the deviceCline
yes you are right , sorry i am talking about my testing case where i am uninstalling the app after registering the device once and then i am getting notifications if i install the app again into the same device without launching it once .Oskar
P
2

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).

Popp answered 2/11, 2015 at 6:39 Comment(1)
Thanks for the answerOskar
P
2

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];
      }
}
Profit answered 2/11, 2015 at 5:19 Comment(1)
thanks for the answer but this is not the answer i am looking for :(Oskar
P
2

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).

Popp answered 2/11, 2015 at 6:39 Comment(1)
Thanks for the answerOskar
I
2
  1. if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )

    • If the app is in killed state then the push notification payload can only be handed over to the app when the user taps the push notification itself. If the user launches the app from app icon then the notification payload will not be passed to the application
  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

    • if you are targeting iOS7 and up then you need to enable background mode for remote notifications. Please check below link in order to get notification payload even when app is in background

    didReceiveRemoteNotification not working in the background

    The above mentioned app delegate method gets called when the app is in foreground,Background and suspended state.

There is no way to get the notification payload when app is killed and when the app icon is directly clicked instead of push notification in notification center.

Inhospitality answered 2/11, 2015 at 10:11 Comment(0)
V
0

Enable "Remote Notifications" under background modes in capabilities in target settings. This will fetch the notification data even when the app is in the background. Also, make sure to implement:

-(void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler;

in your app delegate.

Vercelli answered 14/10, 2017 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.