Push Notification helps you in sending event or alert to your app users even when they are not logged into the app. App42 uses NokiaX Notification API to send Push Notification on NokiaX devices. You can send push notifications from UI console as well as can use APIs to send and schedule Push notification to your app users. App42 also gives you interesting insight about analytics of push messages too. Here are the few easy steps to get started with an App42 Android Push Notification using a sample project in a few minutes.
Register with App42 platform
Create an app once you are on Quick-start page after registration.
If you are already registered, login to AppHQ console and create an app from App Manager -> App Create link.
Open Nokia Notifications developer console . Login with your credentials or register accordingly.
Click on Create services button on right side.
Fill necessary information to create NokiaX service and click on create button.
Now you get your Authorization key of NokiaX.
From AppHQ console click on Push Notification and select Android Settings in Settings option.
Select your app and provider as NokiaX,copy Authorization key that is generated in above step.(please copy value of key not copy ‘key=’ text in that key).
Download sample project from our GitHub Repo
Unzip the downloaded file and import it in eclipse.
Open CommonUtilities.java file and place your Nokia SenderId generated from in previous step.
private static final String NokiaXSenderId = "<Your NokiaX SenderId>";
Open MainActivty.java file in which you need to pass the required information in onCreate method like API KEY & SECRET KEY which you have received after the success of app creation from AppHQ Console, Logged In User is basically that user for which you want to register your device to push as shown below:
App42API.initialize(this,"<YOUR API KEY>","<YOUR SECRET KEY>"); App42API.setLoggedInUser("<Logged In User>") ;
The Above snippet will do all the stuff for you seamlessly. Once app will get started it will register your app with NokiaX as well as with App42 platform for push notification usage. It uses a storeDeviceToken method to register app with App42 server. This is only one time activity and will be done when the app will be opened.
If you want to go into detail about the sample code, please see CommonIntentServiceImpl.java which does all NokiaX Notification related work like registering with NokiaX and App42 server and generating notification. Once your app gets registered with Nokia, its onRegister method gets invoked, which further calls registerWithApp42 method defined in the class as shown below:
public void onRegistered(Context context, String registrationId,DeviceType deviceType) { Log.i(mTag, "Device registered with ID \"" + registrationId + "\""); registerWithApp42(registrationId, deviceType); } private void registerWithApp42(String regId, DeviceType deviceType) { App42Log.debug(" Registering on Server ...."); App42API.buildPushNotificationService().storeDeviceToken(App42API.getLoggedInUser(), regId, deviceType,new App42CallBack() { @Override public void onSuccess(Object paramObject) { // TODO Auto-generated method stub App42Log.debug(" ..... Registration Success ...."); } @Override public void onException(Exception paramException) { App42Log.debug(" ..... Registration Failed ...."); App42Log.debug("storeDeviceToken : Exception : on start up "+ paramException); } }); }
Android Manifest Settings
Following Permissions and entries are required to integrate Push Notification in your project. Sample Android Manifest contained these entries already , however if you are integrating in your existing project put these lines in your Manifest too. You have to replace <YOUR_APPLICATION_PACKAGE_NAME> with the value of your package name. For example if your application package name is com.abc.myapp, you have to use it in place of <YOUR_APPLICATION_PACKAGE_NAME> defined in below Manifest:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="<YOUR_APPLICATION_PACKAGE_NAME>.permission.C2D_MESSAGE" /> <uses-permission android:name="com.nokia.pushnotifications.permission.RECEIVE" /> <permission android:name="<YOUR_APPLICATION_PACKAGE_NAME>.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <receiver android:name="com.shephertz.app42.common.push.plugin.App42NokiaReceiver" android:permission="com.nokia.pushnotifications.permission.SEND" > <intent-filter> <!-- Receives the actual messages. ![bullet](http://cdn.shephertz.com/repository/files/760a0c04733f8d19bbec63cc5caebd585466ab0c2e63eed9219d601266a20eb9/e2a219c85f75b4c1a4a6fac05c9391a571cb5a5e.png) <action android:name="com.nokia.pushnotifications.intent.RECEIVE" /> <!-- Receives the registration id. ![bullet](http://cdn.shephertz.com/repository/files/760a0c04733f8d19bbec63cc5caebd585466ab0c2e63eed9219d601266a20eb9/e2a219c85f75b4c1a4a6fac05c9391a571cb5a5e.png) <action android:name="com.nokia.pushnotifications.intent.REGISTRATION" /> <category android:name="<YOUR_APPLICATION_PACKAGE_NAME>" /> </intent-filter> </receiver> <service android:name="com.shephertz.app42.common.push.plugin.App42NokiaService" /> <meta-data android:name="onMessageOpen" android:value="com.example.android.nokia.app42.push.MainActivity"/>
Build your project and run.
After the success of app run, you can verify your registered user under Push Notification -> Users section and send the message as shown in below steps.
Once your app is registered you are ready to send message to your app user using following code snippet from any App42 SDK (Android/Java/WP etc)
From API
PushnotificationService pushnotificationService = App42API.buildPushnotificationService(); PushNotification pushNotification = pushNotificationService.sendPushMessageToUser(userName,message);
From AppHQ Console
You can also send push messages to your registered app user directly from AppHQ console. To do this, go to AppHQ console and click on Push Notification -> Click on Users -> Select your App -> select your user from table and click on send push button. This will open a pop up to type your message, once you are done click on send button. This will trigger a push notification message to your app user.
If you want to send push message with sound and badge effect on iOS device, you have to pass JSON string in below format
PushnotificationService pushnotificationService = App42API.buildPushnotificationService(); String userName = "Nick"; //Message format for iOS device with sound and badge String message = "{'alert':'Hi There...','badge':1,'sound':'default'}"; PushNotification pushNotification = pushNotificationService.sendPushMessageToUser(userName,message);
You can also do it from AppHQ by putting this JSON format in message text area for sending message to iOS device with sound and badge effect.
App42 Push notification also supports channel subscription model where user can subscribe on channel of his interest to receive the notification. You can send message to channel which will deliver message to all users who are subscribed to that channel. A channel can be created from AppHQ console. Go to Push Notification ->Click on Channels -> Select your App -> Click on add channel button
Once channel is created you can ask user for subscription on the channel. Below is the code snippet for the same
// // First Subscribe User to Channel // String channelName = "cricket channel"; String userName = "Nick"; PushNotification pushNotification = pushNotificationService.subscribeToChannel(channelName,userName); // // Now send message on the Channel // String channelName = "cricket channel"; String message = "Mumbai Indians won IPL 6"; PushNotification pushNotification = pushNotificationService.sendPushMessageToChannel(channelName,message);
App42 Push Notification Service has introduced API to target users who have specific profile/preference. This API requires user preference or profile persistence in Storage Service as a prerequisite. To identify a specific user segmentation, your app user profile or preference information should be saved on App42 cloud e.g. if you are building a music streaming app and you want to send push notifications to all those users who like Rock music, you should have preference of user stored in App42 cloud already and it can be done using Storage Service as shown below.
Saving User Preference
//Set Logged in User Name App42API.setLoggedInUser("USER_NAME"); String profileJSON = "{\"preference\":\"Rock\",\"age\":30,\"company\":\"ShepHertz\"}"; StorageService storageService = App42API.buildStorageService(); storageService.insertJSONDocument("DB_NAME", "COLLECTION_NAME", profileJSON );
Sending Push to Segmented User
Once your app user’s profile/preference information is saved, you can send Push Notification to the segmented user who meets specific criteria (Assuming your app user is already subscribed for push notification. For example, here you can send Push Notification to segmented users who likes Rock music as explained below.
Query targetQuery = QueryBuilder.build("preference","Rock", Operator.EQUALS); String message = "New Album of Rock Music has just Arrived. Please check it out here!!"; PushNotificationService pushNotificationService = App42API.buildPushNotificationService(); pushNotificationService.sendPushToTargetUsers(message, "DB_NAME", "COLLECTION_NAME", targetQuery);
You can also schedule message to your app user on specified time from AppHQ console. To do this, go to Push Notification -> Click on Users -> Select your app -> select target users/device and click on schedule push button. Now type your message in pop up and pass your time to schedule the message.
Message scheduling on channel can be done in similar way. Go to Push Notification -> Click on Channels -> Select your app -> select target channel and click on schedule push button. Now type your message in pop up and pass your time to schedule the message.
Push notification received on user device can be configured and can be controlled from the code written in generateNotification method as shown below
Notification notification = new NotificationCompat.Builder(context) .setContentTitle(context.getString(R.string.app_name)) .setContentText(text).setContentInfo(info).setTicker(text) .setContentIntent(intent).setSmallIcon(icon).setWhen(when) .setLights(Color.YELLOW, 1, 2).setAutoCancel(true).build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE;
You can specify which Activity should be opened once user clicks on message. Its configured inside AndroidManifest.xml as shown below
<meta-data android:name="onMessageOpen" android:value="com.example.android.nokia.app42.push.MainActivity"/>
Push notification goes from App42 server to Nokia server and then device. Delivery of Push notification is not guaranteed from Nokia to NokiaX device and it is stated as best effort. Once it is delivered on user device, there might be a chance that user clears it without actually Opening the message.
Using App42 Push Analytics, you can track how many push notification was sent from your side, how many were delivered and how many were opened by the user. You can see these analytics from AppHQ console which will give a better insight of push notification campaign.
If you want to track push message read/opened event you have to simply put following snippet after Message is clicked and lands to your activity. This will enable tracking of push message and analytics can be seen from AppHQ console.
App42API.buildLogService().setEvent("Message", "Opened", new App42CallBack() { public void onSuccess(Object arg0) { // TODO Auto-generated method stub } public void onException(Exception arg0) { // TODO Auto-generated method stub } });
Once your device is registered for Push Notification you are ready to send multilingual(UTF-8) push message to your app user using following code snippet from any App42 SDK (Android/Java/WP etc)
String userName = "Nick"; String message = "Message which you have to send"; HashMap<String, String> otherMetaHeaders = new HashMap<String, String>(); otherMetaHeaders.put("dataEncoding", "true"); pushNotificationService.setOtherMetaHeaders(otherMetaHeaders) pushNotificationService.sendPushMessageToUser(userName, message, new App42CallBack() { public void onSuccess(Object response) { PushNotification pushNotification = (PushNotification)response; System.out.println("userName is " + pushNotification.getUserName()); System.out.println("Message is " + pushNotification.getMessage()); System.out.println("Expiry is"+pushNotification.getExpiry()); } public void onException(Exception ex) { System.out.println("Exception Message"+ex.getMessage()); } });
For more details of Push Notification Documentation