Implementing Feature

Both Local and remote/push notifications are wrapped under CrossPlatformNotification instance. This makes it seamless while using notification irrespective of its nature.

When you receive any Notification, you will get a callback event with CrossPlatformNotification instance as callback data.

If you want to add any custom Sounds or Icons/Images to the notification, make sure you keep them in the following folders and refer their file name in the Payload or in CrossPlatformNotification instance.

Folder Path

Description

Assets/PluginResources/NativePlugins/iOS

Keep iOS specific resources in this folder

Assets/PluginResources/NativePlugins/Android

Keep Android specific resources in this folder

Assets/PluginResources/NativePlugins/Common

Keep resources common to all platforms in this folder

Register events in OnEnable and De-Register in OnDisable in your NotificationListener.

// Register for callbacks
void OnEnable()
{
//Triggered when registration for remote notification event is done.
NotificationService.DidFinishRegisterForRemoteNotificationEvent += DidFinishRegisterForRemoteNotificationEvent;
//Triggered when local notification was clicked for launching the app.
NotificationService.DidLaunchWithLocalNotificationEvent += DidLaunchWithLocalNotificationEvent;
//Triggered when remote notification was clicked for launching the app.
NotificationService.DidLaunchWithRemoteNotificationEvent += DidLaunchWithRemoteNotificationEvent;
//Triggered when a local notification is received.
NotificationService.DidReceiveLocalNotificationEvent += DidReceiveLocalNotificationEvent;
//Triggered when a remote notification is received.
NotificationService.DidReceiveRemoteNotificationEvent += DidReceiveRemoteNotificationEvent;
}
//Un-Registering once done.
void OnDisable()
{
NotificationService.DidFinishRegisterForRemoteNotificationEvent -= DidFinishRegisterForRemoteNotificationEvent;
NotificationService.DidLaunchWithLocalNotificationEvent -= DidLaunchWithLocalNotificationEvent;
NotificationService.DidLaunchWithRemoteNotificationEvent -= DidLaunchWithRemoteNotificationEvent;
NotificationService.DidReceiveLocalNotificationEvent -= DidReceiveLocalNotificationEvent;
NotificationService.DidReceiveRemoteNotificationEvent -= DidReceiveRemoteNotificationEvent;
}

This should be first step for using notifications. Types allowed are

  • NotificationType.Alert - Shows up alert/banner on receiving a notification.

  • NotificationType.Badge - Shows a badge on receiving a notification, if set.

  • NotificationType.Sound - Plays sound on receiving a notification.

  • NotificationType.None - None of the above.

Set the type you want to set. You can mix above properties and set as well.

NPBinding.NotificationService.RegisterNotificationTypes(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);

Local notifications doesn't need any server/network to get triggered. Thus making it useful when you need any time based event notifications on device.

Steps for scheduling a Local Notification :

  1. Create an instance of CrossPlatformNotification.

  2. Populate the properties as required.

  3. Schedule it by calling ScheduleLocalNotification method.

  4. Receive NotificationService.DidReceiveLocalNotificationEvent callback once a notification is received. If App is launched from notification, NotificationService.DidLaunchWithLocalNotificationEvent is triggered.

//Create an instance of CrossPlatformNotification and fill with details.
private CrossPlatformNotification CreateNotification (long _fireAfterSec, eNotificationRepeatInterval _repeatInterval)
{
// User info - Is used to set custom data. Create a dictionary and set your data if any.
IDictionary _userInfo = new Dictionary<string, string>();
_userInfo["data"] = "add what is required";
// Set iOS specific properties
CrossPlatformNotification.iOSSpecificProperties _iosProperties = new CrossPlatformNotification.iOSSpecificProperties();
_iosProperties.HasAction = true;
_iosProperties.AlertAction = "alert action";
// Set Android specific properties
CrossPlatformNotification.AndroidSpecificProperties _androidProperties = new CrossPlatformNotification.AndroidSpecificProperties();
_androidProperties.ContentTitle = "content title";
_androidProperties.TickerText = "ticker ticks over here";
_androidProperties.LargeIcon = "NativePlugins.png"; //Keep the files in Assets/PluginResources/VoxelBusters/NativePlugins/Android folder.
// Create CrossPlatformNotification instance
CrossPlatformNotification _notification = new CrossPlatformNotification();
_notification.AlertBody = "alert body"; //On Android, this is considered as ContentText
_notification.FireDate = System.DateTime.Now.AddSeconds(_fireAfterSec);
_notification.RepeatInterval = _repeatInterval;
_notification.UserInfo = _userInfo;
_notification.SoundName = "Notification.mp3"; //Keep the files in Assets/PluginResources/NativePlugins/Android or iOS or Common folder.
_notification.iOSProperties = _iosProperties;
_notification.AndroidProperties = _androidProperties;
return _notification;
}

Schedule the created notification.

CrossPlatformNotification _notification = CreateNotification(FIRE_AT_SECS, REPEAT_INTERVAL);
//Schedule this local notification.
NPBinding.NotificationService.ScheduleLocalNotification(_notification);

Allowed REPEAT_INTERVAL options are

  • eNotificationRepeatInterval.NONE

  • eNotificationRepeatInterval.MINUTE

  • eNotificationRepeatInterval.HOUR

  • eNotificationRepeatInterval.DAY

  • eNotificationRepeatInterval.WEEK

  • eNotificationRepeatInterval.MONTH

  • eNotificationRepeatInterval.YEAR

Remote notifications require a server or a service (check onesignal.com) to push your content to APNS or GCM servers.

Once APNS/GCM servers receive the payload, it will send the payload to the registered device. So, for receiving your device need to register with APNS/GCM ahead.

Once a remote notification is received, NotificationService.DidReceiveRemoteNotificationEvent is triggered. Incase you launch the app from notification, NotificationService.DidLaunchWithRemoteNotificationEvent is triggered.

Register & Un-Register

You need to register for remote notifications to get a device token.

On Android, you need to set Sender ID to register to GCM

NPBinding.NotificationService.RegisterForRemoteNotifications();

Info

Remote notifications comes with a overhead on battery. So, register it only if you are using it.

Once registration is successful, NotificationService.DidFinishRegisterForRemoteNotificationEvent is called with the device token.

You need to pass the device token received to your server.

In-case, if you don't need remote notifications anymore, you can un-register.

NPBinding.NotificationService.UnregisterForRemoteNotifications();

Payload format for Android with default keys from NPSettings.

{
"registration_ids":[
"APA91bE38IGujnSN5.."
],
"data":{
"content_title" : "Title here"
"content_text":"Content text here...."
"ticker_text" : "Ticker text shown in status bar goes here"
"tag" : "OptionalTag - This needs to be diff if you want to overwrite previous notification"
"custom-sound" : "notification.mp3"
"large-icon":"NativePlugins.png"
"badge": 5 - Set a number over here to display badge on the app icon
"user_info":
{
"key1" : "value1"
"key2" : "value2"
}
}
}

Payload format for iOS with default keys from NPSettings.

//Payload format for iOS with default user_info key.
{
"aps" : {
"alert” : {
“body” : "message goes here”,
“action-loc-key” : “VIEW”,
"actions" : [
{
“id" : “delete",
"title" : "Delete"
},
{
“id" : “reply-to”,
"loc-key" : “REPLYTO”,
"loc-args" : [“Jane"]
}
]
}
"badge" : 3,
"sound" : “notification.mp3"
},
"user_info" : {
"key1":"value1",
"key2":"value2"
},
}