Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Push Notification in IBM MobileFirst Xamarin SDK

654 vues

Publié le

IBM MobileFirst Platform Foundation Server (aka Worklight) has a unified push notification capability. This presentation walks you thru the detailed steps to configure your Xamarin.Android app to receive Push notifications.

Publié dans : Mobile
  • Identifiez-vous pour voir les commentaires

Push Notification in IBM MobileFirst Xamarin SDK

  1. 1. ® IBM Software Group © 2014 IBM Corporation Using the IBM MobileFirst Platform Foundation (MFP) Xamarin SDK Push Notifications for Android @ajaychebbi IBM MobileFirst Platform Foundation
  2. 2. 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 vendor  Google Cloud Messaging service (GCM)  Apple Push Notification service (APNS)  Windows Notification Service (WNS)
  3. 3. 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
  4. 4. 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_notificati ons_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)
  5. 5. IBM Software Group | Cloud - MobileFirst Innovation for a smarter planet 5 Push Notifications – MFP setup  Copy WorklightSampleworklightWorklightSampleappsandroidWorklightSamplepush. png to Resourcesdrawable  A sample adapter is shipped with the component that will be used to manage the server side message sending etc – Copy the componentworklightAssetsPushAdapter sample to WorklightSampleworklightWorklightSampleadapters –Create a security test “MySecurityTest” in XtestworklightXtestserverconfauthenticationConfig.xml under <securityTests> <securityTests> <mobileSecurityTest name="MySecurityTest"> <testUser realm="SampleAppRealm"/> <testDeviceId provisioningType="none"/> </mobileSecurityTest> . . </securityTests>
  6. 6. IBM Software Group | Cloud - MobileFirst Innovation for a smarter planet 6 Push Notifications – Worklight setup  Add the Google API Key and sender ID in WorklightSampleworklightWorklightSampleappsandroidWorklightSam pleapplication-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 assetswlclient.properties #For Push Notifications,uncomment below line and assign value to it GcmSenderId = YOUR_GCM_ID
  7. 7. 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.
  8. 8. IBM Software Group | Cloud - MobileFirst Innovation for a smarter planet 8 Push Notifications – App Configuration Define Permissions in PropertiesAssemblyInfo.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")]
  9. 9. IBM Software Group | Cloud - MobileFirst Innovation for a smarter planet 9 Push Notifications – App Configuration BroadcastReceiver and Intent Define BroadcastReceiver and Intent Service in propertiesAndroidManifest.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>
  10. 10. IBM Software Group | Cloud - MobileFirst Innovation for a smarter planet 10 Push Notifications – App Configuration Define 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.cs protected 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 (); }
  11. 11. 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:devworkspacesXtestworklightXtest>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
  12. 12. 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
  13. 13. 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();
  14. 14. 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>()); }
  15. 15. IBM Software Group | Cloud - MobileFirst Innovation for a smarter planet 15 Push Notifications – API Listener gets the notifications public class NotificationListener:WorklightPushNotificationListener { public void OnMessage(JsonObject NotificationProperties, JsonObject Payload) { Console.WriteLine ("Got notification!"); Console.WriteLine (NotificationProperties.ToString ()); } }
  16. 16. IBM Software Group | Cloud - MobileFirst

×