Uiapplicationlaunchoptionsremotenotificationkey Not Getting Userinfo

Push Notification is not working in UIApplicationLaunchOptionsRemoteNotificationKey

Go to project > Capabilities, Turn on Background Modes and enable Remote notifications

Sample Image

and then in app delegates simply add following method

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
self.notificationHandlingFunction(remoteNotification)
}

will work for all cases

How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems

So it turned out the whole method signature has changed and when I implemented the new signature things worked just fine. Below is the code.

new didFinishLaunchingWithOptions method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

//and then
if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {

// Do what you want to happen when a remote notification is tapped.

}

}

Hope this helps.

iOS: How to get all of the push notification userinfo while your App is completely closed and reopened?

Unfortunately, it's not currently possible client side with that method to query old notifications that have occurred while the app was completely closed. See this question: didReceiveRemoteNotification when in background.

A way around it is to keep track of which notifications you send from your server per user. When didReceiveRemoteNotification: is called, you can take that notification and compare it against the server's messages for the current user. If one of them matches, mark it some way on the server. That way, if there are messages sent when your app is backgrounded, you can query for messages that haven't been marked from the server and get all 'missed' notifications.

Accessing push payload if app is inactive

You can check this in didFinishLaunching using UIApplicationLaunchOptionsRemoteNotificationKey as launch options.

UIApplicationLaunchOptionsRemoteNotificationKey: Indicates that a
remote notification is available for the app to process. The value of
this key is an NSDictionary containing the payload of the remote
notification. > - alert: Either a string for the alert message or a
dictionary with two keys: body and show-view. > - badge: A number
indicating the quantity of data items to download from the provider.
This number is to be displayed on the app icon. The absence of a badge
property indicates that any number currently badging the icon should
be removed. > - sound: The name of a sound file in the app bundle to
play as an alert sound. If “default” is specified, the default sound
should be played.

You can call application:didReceiveRemoteNotification: in application:didFinishLaunchingWithOptions: manually.

Objective C

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

if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}

return YES;
}

Swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

self.application(application, didReceiveRemoteNotification: launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey]! as! [NSObject : AnyObject])

}

return true
}

Detect if the app was launched/opened from a push notification

See This code :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//opened from a push notification when the app was on background
}
}

same as

-(void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification


Related Topics



Leave a reply



Submit