Push Notifications

Setup

Before you can use Remote (Push) notification with the IOS Native plugin, you need to enable this API with the editor settings. Navigate to the:

Windows → Stan's Assets → IOS Native → Edit Settings

and enable push notifications API. Make sure you do not leave it enabled without using a push notification in your build. It may cause App Store to review problems.

Implementation

First of all, you need to request push notifications permission for your  app and retrieve the device token. See the code example below:

ISN_RemoteNotificationsController.Instance.RegisterForRemoteNotifications ((ISN_RemoteNotificationsRegistrationResult result) => {
    if(result.IsSucceeded) {
        Debug.Log ("DeviceId: " + result.Token.DeviceId);
    } else {
        Debug.Log ("Error: " + result.Error.Code + " / " + result.Error.Message);
    }
});

Once Token is received, you may send this device token to your server, and now it will be able to send a remote notification to this device.  Please, note that you can always find out current user notification Settings.

If you need help with your application configuration inside iTunes Connect and Xcode, please, point your browser to a Setup and test push notifications  guide.

Remote notifications in runtime

If you want to know if an app has received a push notification in runtime, you can subscribe to the OnRemoteNotificationReceived action, as showed in the code snippet below.

ISN_RemoteNotificationsController.OnRemoteNotificationReceived  += HandleOnRemoteNotificationReceived;

void HandleOnRemoteNotificationReceived (ISN_RemoteNotification notification){
    IOSMessage.Create("Remote Notification Received", "Body: " + notification.Body);
}
 

If you app has been launched from the notification, you will get the notification object from the LaunchNotification getter. If your app has launched normally, the LaunchNotification will remain null. Code snippet below shows how to check if an app has been launched from the local notification and print notification data.

if(ISN_RemoteNotificationsController.Instance.LaunchNotification != null) {
	ISN_RemoteNotification notification = ISN_RemoteNotificationsController.Instance.LaunchNotification;
	IOSMessage.Create("Launch Remote Notification", "Body: " + notification.Body);
}

Note: 

The device token is your key to sending push notifications to your app on a specific device. Device tokens can change, so your app needs to reregister every time it is launched and pass the received token back to your server. If you fail to update the device token, remote notifications might not make their way to the user’s device. Device tokens always change when the user restores backup data to a new device or computer or reinstalls the operating system. When migrating data to a new device or computer, the user must launch your app once before remote notifications can be delivered to that device.