Detect If the App Was Launched/Opened from a Push Notification

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

Detect whether React Native iOS app was opened via push notification

There are two cases here that need to be detected in different ways:

  1. The app has been completely terminated (e.g. by restarting the phone, or by double-tapping home and swiping it off the list of apps running in the background) and is being launched by a user's tap on a push notification. This can be detected (and the notification's data acquired) via the React.PushNotificationIOS.getInitialNotification method.
  2. The app had been suspended and is being made active again by a user's tap on a push notification. Just like in a native app, you can tell that this is happening because iOS passes the tapped notification to your app when it is opening (even if it's an old notification) and causes your notification handler to fire while your app is in UIApplicationStateInactive state (or 'background' state, as React Native's AppStateIOS class calls it).

Code to handle both cases (you can put this in your index.ios.js or somewhere else that's run on app launch):

import React from 'react';
import { PushNotificationIOS, AppState } from 'react-native';

function appOpenedByNotificationTap(notification) {
// This is your handler. The tapped notification gets passed in here.
// Do whatever you like with it.
console.log(notification);
}

PushNotificationIOS.getInitialNotification().then(function (notification) {
if (notification != null) {
appOpenedByNotificationTap(notification);
}
});

let backgroundNotification;

PushNotificationIOS.addEventListener('notification', function (notification) {
if (AppState.currentState === 'background') {
backgroundNotification = notification;
}
});

AppState.addEventListener('change', function (new_state) {
if (new_state === 'active' && backgroundNotification != null) {
appOpenedByNotificationTap(backgroundNotification);
backgroundNotification = null;
}
});

Detect if app was launched by opening a local notification on iOS10+

Make sure you're assigning your implementation of UNUserNotificationCenterDelegate to UNUserNotificationCenter.current() before your app finishes launching, as it says in the documentation.

Then you should be getting a call to your UNUserNotificationCenterDelegate's userNotificationCenter(_:didReceive:withCompletionHandler:) at app launch with the response object, which contains the original notification. For me, it happened some time after my application(_:didFinishLaunchingWithOptions:) was called.

Detect if App was Launched From Tapping Local Notification Swift

Look in the options for your scene delegate willConnect method or (if there is no scene delegate) your app delegate willFinishLaunching method. This tells you how you got launched.

Detect If push notifications were received when app is not launched and user didn't open the app through them

In your AppDelegate do this (sorry for swift implementation):

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// We see if we have a recent push notification date stored in the user defaults.
// If there hasn't been a recent one sent this if block will not be entered.
if let mostRecentPushNotificationDate = NSUserDefaults.standardUserDefaults().objectForKey("mostRecentPushNotification") as? NSDate {

// We find the difference between when the notification was sent and when the app was opened
let interval = NSDate().timeIntervalSinceDate(mostRecentPushNotificationDate)

// Check to see if we opened from the push notification
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
print("we opened from the push notifcation and the time difference was \(interval) seconds.")
}

// We did not open from a push notification
else {
print("we opened from the spring board and the time difference was \(interval) seconds.")
}

// We remove the object from the store so if they open the app without a notification being sent we don't check this if block
NSUserDefaults.standardUserDefaults().removeObjectForKey("mostRecentPushNotification")
}


return true
}

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

// Set a flag when a push notification was sent
NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey: "mostRecentPushNotification")
}

iOS push notification: how to detect if the user tapped on notification when the app is in background?

OK I finally figured out.

In the target settings ➝ Capabilities tab ➝ Background Modes, if you check "Remote Notifications", application:didReceiveRemoteNotification: will get triggered as soon as notification arrives (as long as the app is in the background), and in that case there is no way to tell whether the user will tap on the notification.

If you uncheck that box, application:didReceiveRemoteNotification: will be triggered only when you tap on the notification.

It's a little strange that checking this box will change how one of the app delegate methods behaves. It would be nicer if that box is checked, Apple uses two different delegate methods for notification receive and notification tap. I think most of the developers always want to know if a notification is tapped on or not.

Hopefully this will be helpful for anyone else who run into this issue. Apple also didn't document it clearly here so it took me a while to figure out.

Sample Image



Related Topics



Leave a reply



Submit