Push Notification On Android
Solution 1:
On Device, you need to perform following steps :
Register to GoogleCloudMessaging.
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); String gcmRegistrationId = gcm.register();
Send gcmRegistrationId got in step 1 to Server. Server will use this id to send GCM tickle.
Register GCM Receiver. Add it in AndroidManifest.xml as below :
<receiverandroid:name="com.hp.msa.receiver.GCMReceiver"android:exported="true"android:permission="com.google.android.c2dm.permission.SEND" ><intent-filter><actionandroid:name="com.google.android.c2dm.intent.RECEIVE" /><actionandroid:name="com.google.android.c2dm.intent.REGISTRATION" /></intent-filter></receiver>
GCMReceiver.java will look as below :
publicclassGCMReceiverextendsWakefulBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
// Here, you will get actual PUSH notification.// After receiving it, you can perform your tasks// Intent contains data sent by server GoogleCloudMessaginggcm= GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received// in your BroadcastReceiver.StringmessageType= gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
// Logic
} elseif (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
// Logic
} elseif (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// Logic
}
}
}
Solution 2:
The process of sending and receiving push notifications is rather complex as it involves several parties. If you are using google server to broadcast your request (which I think you should be doing), make sure that you server IP is added to the push notification in google API console. If your phone is registering with a push server, it should receive some kind of intent within a minute after sending the push. You can try to debug it, connect your phone to PC, connect debugger, remove all filters and in the debug console of you IDE look for any intent occurrence that could be google push notification. If you receive any, that means that you application is unable to receive the injected intent.
You can checkout aerogear project, which provides a ready push server which is able to register phone installations and forward notifications to google server. It also provides basic web console for managing the server. Aerogear web site also contains many tutorials and examples of working apps for android and other platforms.
https://github.com/aerogear/aerogear-push-helloworld/tree/master/android
Good luck! Push notifications take some time but not because of the complexity but the amount of things you have to do.
Post a Comment for "Push Notification On Android"