Push Notification in IBM MobileFirst Xamarin SDK

Post on 13-Apr-2017

422 views 0 download

Transcript of Push Notification in IBM MobileFirst Xamarin SDK

®

IBM Software Group

© 2014 IBM Corporation

Using the IBM MobileFirst Platform Foundation (MFP) Xamarin SDK Push Notifications for Android

@ajaychebbi IBM MobileFirst Platform Foundation

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 2

Push Notifications - the basics

Notifications come to your device via the Cloud services hosted by the OS vendorGoogle Cloud Messaging service (GCM)

Apple Push Notification service (APNS)

Windows Notification Service (WNS)

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 3

Push Notifications - the basics

You have to do a bunch of handshakes and registrations e.g. above is for GCM

Eventually the notification message goes from “your Server”

There is also the maintenance of devices and users and un-installs etc.

Excellent info on how it works at Xamarin.com

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 4

Push Notifications – pre reqs

This is going to be a long process to understand – so hang in there

More info here http://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/

Setup the Google API project and get the senderID and key

– If you dont have it, get it at https://code.google.com/apis/console

Also make sure you have the “Google APIs” emulator image (a real device works much faster)

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 5

Push Notifications – MFP setup Copy WorklightSample\worklight\WorklightSample\apps\

androidWorklightSample\push.png to Resources\drawable

A sample adapter is shipped with the component that will be used to manage the server side message sending etc

– Copy the component\worklightAssets\PushAdapter sample to WorklightSample\worklight\WorklightSample\adapters

–Create a security test “MySecurityTest” in Xtest\worklight\Xtest\server\conf\authenticationConfig.xml under <securityTests>

<securityTests> <mobileSecurityTest name="MySecurityTest">

<testUser realm="SampleAppRealm"/> <testDeviceId provisioningType="none"/> </mobileSecurityTest>..</securityTests>

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 6

Push Notifications – Worklight setup

Add the Google API Key and sender ID in WorklightSample\worklight\WorklightSample\apps\androidWorklightSample\application-descriptor.xml

Also add the securityTest as “MySecurityTest”<nativeAndroidApp id="androidXtest" platformVersion="6.2.0.00.20140825-1637"

version="1.0" xmlns="http://www.worklight.com/native-android-descriptor" securityTest="MySecurityTest">

<displayName>androidXtest</displayName><description>androidXtest</description><pushSender key="YOUR_GCM_KEY" senderId="YOUR_GCM_ID"/> <publicSigningKey></publicSigningKey><packageName></packageName>

</nativeAndroidApp>

Add the GCMSenderID to assets\wlclient.properties#For Push Notifications,uncomment below line and assign value to itGcmSenderId = YOUR_GCM_ID

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 7

Push Notifications – App

Android application requires the following three things:

•Permissions - An Android application must be granted permission to use the internet and to receive messages from Google Cloud Messaging.

•BroadcastReceiver - A BroadcastReceiver must be configured to listen for the Intents that the Google Services Framework will publish when a message is received from Google Cloud Messaging.

•IntentService - The BroadcastReceiver will not handle the Intents itself, instead it will invoke an IntentService that will process the messages.

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 8

Push Notifications – App Configuration

Define Permissions in Properties\AssemblyInfo.cs// This will prevent other apps on the device from receiving GCM messages for this app

// It is crucial that the package name does not start with an uppercase letter - this is forbidden by Android.[assembly: Permission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")][assembly: UsesPermission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]

// Gives the app permission to register and receive messages.[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]

// This permission is necessary only for Android 4.0.3 and below.[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]

// Need to access the internet for GCM[assembly: UsesPermission(Name = "android.permission.INTERNET")]

// Needed to keep the processor from sleeping when a message arrives[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 9

Push Notifications – App Configuration

BroadcastReceiver and Intent

Define BroadcastReceiver and Intent Service in properties\AndroidManifest.xml

Worklight provides a inbuilt broadcast receiver and Intent service – so just add them to the manifest

<service android:name="com.worklight.wlclient.push.GCMIntentService" /> <receiver android:name="com.worklight.wlclient.push.WLBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"><intent-filter>

<action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="worklightsample.android" /> </intent-filter>

<intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="worklightsample.android" /> </intent-filter> </receiver>

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 10

Push Notifications – App ConfigurationDefine a IntentFilter for a activity in the app•Make sure app_name in Strings.xml is WorklightSample.Android

•Edit MainActivity.cs and add the following IntentFilter[IntentFilter (new[]{" worklightsample.android.WorklightSample.Android.NOTIFICATION"} ,

Categories=new[]{Intent.CategoryDefault})]

Format: [package].[app_name from Strings.xml].NOTIFICATION

•Override the lifecycle methods of MainActivity.csprotected override void OnResume ()

{base.OnResume(); wlClient.PushService.Foreground = true;

}protected override void OnPause (){

base.OnPause(); wlClient.PushService.Foreground = false;}protected override void OnDestroy (){

base.OnDestroy(); wlClient.PushService.UnregisterReceivers ();}

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 11

Push Notifications – lets send a message Execute the adapter to send a message

C:\dev\workspaces\Xtest\worklight\Xtest>mfp invoke

[?] Which adapter do you want to use? PushAdapter

[?] Enter the comma-separated parameters: "worklight","Hello!"

Eventually you will use APIs in the adapter to send notifications from your server side application

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 12

Push Notifications – the Notification!

If app is in foreground – the Android activity gets the notification

If the app is in the background – you see the notification on the notification bar

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 13

Push Notifications – API

The first step is to create an instance of the WLClient class:IworklightClient wlClient = Worklight.Xamarin.Android.WorklightClient.CreateInstance (this);

WorklightPushService pushService = wlClient.PushService;

You do all push notification operations from the WorklightPushService

ReadyToSubscribe Event – When connecting to a Worklight Server, the app attempts to register itself with the GCM server to receive push notifications. Called when the registration is complete.

InitRegistration() - To initiate the registration sequence.client.PushService.ReadyToSubscribe += HandleReadyToSubscribe;client.PushService.InitRegistration();

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 14

Push Notifications – API

Use the RegisterEventSourceNotificationCallback method to register an alias on a particular event source.void HandleReadyToSubscribe(object sender, EventArgs a){

Console.WriteLine ("We are ready to subscribe to the notification service!!");client.PushService.RegisterEventSourceNotificationCallback

(pushAlias,"PushAdapter","PushEventSource",new NotificationListener ());client.PushService.SubscribeToEventSource(pushAlias,new

Dictionary<string,string>());}

IBM Software Group | Cloud - MobileFirst

Innovation for a smarter planet 15

Push Notifications – API

Listener gets the notificationspublic class NotificationListener:WorklightPushNotificationListener

{public void OnMessage(JsonObject NotificationProperties, JsonObject Payload){

Console.WriteLine ("Got notification!");Console.WriteLine (NotificationProperties.ToString ());

}}

IBM Software Group | Cloud - MobileFirst