Responding to Notification - StansAssets/com.stansassets.android-native GitHub Wiki

When your app is not running or is in the background, the system automatically delivers local notifications.

If your app is running in the foreground, notifications are delivered directly to your app. You can then decide whether to handle the notification quietly or alert the user. To respond to the delivery of notifications, you must subscribe to the OnNotificationReceived event. See the example below.

using SA.Android.App;
...

AN_NotificationManager.OnNotificationReceived.AddSafeListener(this, (request) => {
    Debug.Log("request.Identifier: " + request.Identifier);
    Debug.Log("User has opended the local notification request with info: " + JsonUtility.ToJson(request));
});

You may also react is user clicked on the notification when your app was in the background mode. When an app is switched from background to foreground, you can act accordingly. For example, if a user clicked on a "reminder" notification to get his daily reward, it's a good practice to bring daily reward screen as soon as your application is switched to the foreground. To respond to the notification click, you must subscribe to the OnNotificationClick event. See the example below.

using SA.Android.App;
...

AN_NotificationManager.OnNotificationClick.AddSafeListener(this, (request) => {
    Debug.Log("request.Identifier: " + request.Identifier);
    Debug.Log("User has opended the local notification request with info: " + JsonUtility.ToJson(request));
});

See the next chapter how to respond if your app was launched using notification.

Handling Notifications When Your App Is in not running

If the user clicked on notification generated by your app, the application will be launched. There is a way to find out for you if your app was launched by notification click. You can use AN_NotificationManager.LastOpenedNotificationRequest as soon as the application is started. If LastOpenedNotificationRequest contains the AN_NotificationRequest object, means your app was launched with a user clicking the notification. So you can act accordingly.

using SA.Android.App;
...

if(AN_NotificationManager.LastOpenedNotificationRequest != null) {
    Debug.Log("Looks like the app was launched from notifications request: " 
        + JsonUtility.ToJson(AN_NotificationManager.LastOpenedNotificationRequest));
}