Android Push Notification

Push Notification helps you in sending event or alert to your App users even when they are not logged into App. App42 uses GCM (Google Cloud Messaging) to send Push Notification on Android devices. Here are the few easy steps to get started with App42 Android Push Notification.

1. Prerequisite Setup

  • Create a project and get your Project Id from google developer console.
  • Click on services option in Google developer console and enable Google Cloud Messaging for Android service.
  • Click on API Access tab and create a new server key for your application with blank server information.
  • Go to AppHQ console and click on Push Notification and select Android Settings in Settings option.
  • Select your app and copy server key that is generated in Google developer console in above step and submit it.
  • android push first step
  • android push arrow
  • android push step first

2. Start building

  • Download sample project from our GitHub Repo.

  • Unzip the downloaded file and import it in eclipse.
  • Open MainActivty.java file in and pass required information in onCreate method as shown below
@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		App42API.initialize(this,"<YOUR API KEY>","<YOUR SECRET KEY>");
		App42API.setLoggedInUser("<Logged In User>") ;
		Util.registerWithApp42("<Your Google Project No>");
	}

The Above snippet will do all the stuff for you seamlessly. Once app will get started it will register your app with GCM as well as with App42 platform for push notification usage. It uses storeDeviceToken method to register app with App42 server. This is only one time activity and will be done when app will be opened.

If you want to go in detail about the sample code, please see GCMIntentService.java which does all GCM related work like registering with GCM and App42 server and generating notification. This class overrides GCMBaseIntentService class and overrides onMessage and onRegister method. Once your app gets registered with GCM, its onRegister method gets invoked, which further calls registerWithApp42 method defined in the class as shown below

@Override
	protected void onRegistered(Context arg0, String regId) {
		Log.i(TAG, "Device registered: regId = " + regId);
		registerWithApp42(regId);
	}
	//Method to get registered with App42 Platform
	private void registerWithApp42(String regId) {
		App42Log.debug(" Registering on App42 Server ....");
		App42API.buildPushNotificationService().storeDeviceToken(App42API.getLoggedInUser(), regId, new App42CallBack() {
			@Override
			public void onSuccess(Object paramObject) {
				App42Log.debug(" ..... Registration Success ....");
				GCMRegistrar.setRegisteredOnServer(App42API.appContext, true);
			}
			@Override
			public void onException(Exception paramException) {
				App42Log.debug(" ..... Registration Failed ....");
				App42Log.debug("storeDeviceToken :  Exception : on startup " +paramException);
			}
		});
	}

3. Sending Message to User

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)

PushnotificationService pushnotificationService = App42API.buildPushnotificationService();    
PushNotification pushNotification = pushNotificationService.sendPushMessageToUser(userName,message);

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 User Link–> Select your App–> select your user from table and click on send message 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.

4. Sending Message to Channel

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–> Channel–> Select your App–> Click on Create 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 = pushServiceNotification.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); 

5. Scheduling Message to User

You can also schedule message to your app user on specified time from AppHQ console. To do this, got to Push Notification–> Click on User link–> Select your app–> select target users/device and click on schedule message button. Now type your message in pop up and pass your time to schedule the message.

6. Scheduling message to Channel

Message scheduling on channel can be done in similar way. Go to Push Notification–> Click on Channel link–> Select your app–> select target channel and click on schedule message button. Now type your message in pop up and pass your time to schedule the message.

7. Configuring Notification UI

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(title)
        .setContentText(message)
        .setContentIntent(intent)
        .setSmallIcon(icon)
        .setWhen(when)
        .setLargeIcon(getBitmapFromURL(LARGE_IMAGE_URL))
        .setLights(Color.YELLOW, 1, 2)
        .setAutoCancel(true)
        .build();

8. Opening Activity on Message Click

You can specify which Activity should be opened once user clicks on message. Its configured inside AndroidManifest.xml as shown below

  <service android:name=".GCMIntentService" >
        <meta-data android:name="onMessageOpen" android:value="com.example.app42sample.MessageActivity" />
  </service>

9. Doing Push Analytics

Push notification goes from App42 server to GCM server and then device. Delivery of Push notification is not guaranteed from GCM to 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.

hubspot cta