ibm-bluemix-mobile-services/bms-clientsdk-cordova-plugin-push

Getting parameters of notification when my application is opened through it?

Closed this issue · 14 comments

Hello,
I use this plugin to enable and receive push notifications normally both with the App active or disabled.
However I could not understand how to get a customized information that I sent in payload when the app is inactive and the user touches on the notification to open the App.

Is there a way to my app receive information that was opened by a notification?

In your Javascript code, you can access the notification payload in the callback function of MFPPush.registerNotificationsCallback:

MFPPush.registerNotificationsCallback(function(notification) {
    alert(notification.payload)
});

Yes, I have this code in the application and the app when is open receiving the payload information. The problem is not when the app is on background, is when this app is not running (closed) and will open through the notification. I do not get this information to perform the required action.

Hi @adrianopaladini , I've tested this and can confirm that when the app is closed I do not get the payload information.

Try adding the following to the didReceiveRemoteNotification function in your AppDelegate.m:

completionHandler(UIBackgroundFetchResultNewData);

Also make sure in your iOS project settings under Capabilities you have enabled Background Modes and checked the box for Remote Notifications.

Please let me know if the issue still persists after trying this.

Hi, the above steps did not solve my problem.

But searching on the web, i see these two links:
http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/
http://stackoverflow.com/questions/12116282/handling-push-notifications-when-app-is-not-running

So, i remove under Capabilities the Remote Notifications from Background Modes, and add the code above:

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

    NSDictionary *info = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    if (info) {
        double delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [[CDVMFPPush sharedInstance] didReceiveRemoteNotification:info];
        });
    }   
   return YES;
}

Not the best way, but It's working.

I made the necessary corrections to solve my problem, and made a Pull Request, where possible, please take a look and check if it helps.

Hi,
I pulled your changes and tested them and everything seems good. I've made a few adjustments and merged them in to master. Thank you for the update.

Hi,
Dose this feature has been published to the npm repository? Now i missed the issue too. Or could you please educate me how to add this to my project manually? I can't write swift and object c. Thanks

@superstonne https://github.com/ibm-bluemix-mobile-services/bms-clientsdk-cordova-plugin-push#objective-c

In the Appdelegate.m file add the following

// Handle receiving a remote notification on launch
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

  -----------
    if (!launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        [[CDVBMSPush sharedInstance] didReceiveRemoteNotificationOnLaunchWithLaunchOptions:launchOptions];
    }
  -----------
}

@AnanthaKrish
Thanks for your comments. But it seems this doesn't work for me. And when i added the code, i start my app, i got an fatal error: Fatal error int CDVBMSPush.swift line 450: Unexpectedly found nil while unwrapping an Optional value
image

@superstonne Hi, add check for the remoteNotifications also . ,

if let remoteNotifications = launchOption..... {
   if remoteNotifications.allKeys...........
}

@AnanthaKrish Hi, does it look like this?
image

func didReceiveRemoteNotificationOnLaunch(launchOptions: NSDictionary?) {
if launchOptions == nil {
return
}
let remoteNotification = launchOptions![UIApplicationLaunchOptionsKey.remoteNotification] as! NSDictionary
if remoteNotification.allKeys.count > 0 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
CDVBMSPush.sharedInstance.didReceiveRemoteNotification(notification: remoteNotification)
}
}
}

It's still not work.

@AnanthaKrish Now the error has gone. But i still can't get the message when in the back mode.

@superstonne

if let remoteNotification = launchOptions![UIApplicationLaunchOptionsKey.remoteNotification] as! NSDictionary {
                if remoteNotification.allKeys.count > 0 {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                        CDVBMSPush.sharedInstance.didReceiveRemoteNotification(notification: remoteNotification)
                    }
                }
            }

@AnanthaKrish This seems doesn't work me. I still can't get the parameter.