Responding to Notification
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.CrossPlatform.Notifications;
...
var client = UM_NotificationCenter.Client;
client.OnNotificationReceived.AddListener((UM_NotificationRequest request) => {
//Notification was received while app is running
});
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.CrossPlatform.Notifications;
...
var client = UM_NotificationCenter.Client;
client.OnNotificationClick.AddListener((UM_NotificationRequest request) => {
//User clicked on notification while app is running
});
See the next chapter how to respond if your app was launched using notification.
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 LastOpenedNotificationRequest as soon as the application is started. If LastOpenedNotificationt contains the UM_NotificationRequest object, means your app was launched with a user clicking the notification. So you can act accordingly.
using SA.CrossPlatform.Notifications;
...
UM_NotificationRequest startRequest = client.LastOpenedNotificationt;
if(startRequest != null) {
//if this isn't null on your app launch, means user launched your app by clicking on notification icon
}
It's good practice to cancel all pending notifications once your app is launched and re-schedule new ones. By implementing this little trick, you can make sure that the user will not get notifications while your application running. There are several methods you can operate.
If you need to remove already delivered notification. You may use the RemoveDeliveredNotification(int Identifier) method for that purpose:
using SA.CrossPlatform.Notifications;
...
var client = UM_NotificationCenter.Client;
client.RemoveDeliveredNotification(100);
Or you can remove all of the delivered notifications with the RemoveAllDeliveredNotifications method.
using SA.CrossPlatform.Notifications;
...
var client = UM_NotificationCenter.Client;
client.RemoveAllDeliveredNotifications();
When you need to unschedule notification that wasn't yet delivered, use RemovePendingNotification(int Identifier) method
using SA.CrossPlatform.Notifications;
...
var client = UM_NotificationCenter.Client;
client.RemovePendingNotification(100);
Or you can remove all of the pending notifications with the RemoveAllPendingNotifications method.
using SA.CrossPlatform.Notifications;
...
var client = UM_NotificationCenter.Client;
client.RemoveAllPendingNotifications();
Reliable and high-quality Unity Development service. Let's Talk!
Website | AssetStore | LinkedIn | Youtube | Scripting Reference
- Application
- Dark Mode
- Native Dialogs
- Native Preloader
- Date Picker Dialog
- Time Picker Dialog
- Rate Us Dialog
- Build Info
- Locale
- Permissions
- Wheel Picker Dialog
- Getting Started
- Connecting to The Service
- Purchase flow
- Transactions Validation
- Test Inside the Editor
- Advanced use cases
- Getting Started
- Enabling the Ads Service
- Banner Ads
- Non-rewarded Ads
- Rewarded Ads
- Unity Ads
- Google AdMob
- Chartboost
- Getting Started
- Analytics API
- Automatic Tracking
- Unity Analytics
- Firebase Analytics
- Facebook Analytics