I am trying to implement push notification on my android app. So no better place to start then the Googles Developers page.
I am trying to follow this tutorial here : GCM Demo App . Tutorial suggests to use the sample code that is given through the SDK Manager. After doing so and trying to send a push notification , when the app is running i see on the screen being written that a new push has arrived.
However , when the app is on background or not running i dont get a push notification. If i open the app , again the messages are shown on the screen. But i never get anything in the form of notification with a popup and a sound.
Do i manually have to do this in android? I thought it would be similar to iOS where the platform is responsible for showing you the notification.
Any ideas how i can implement it?
But i never get anything in the form of notification with a popup and a sound.
The sound can be programmed into the code when a notification is triggered. Say with something like this.
Notification notification = new Notification(icon, tickerText, when);
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
However , when the app is on background or not running i dont get a push notification
Do i manually have to do this in android? I thought it would be similar to iOS where the platform is responsible for showing you the notification.
Your app is always sent the GCM data (push notifications). How you handle that data is up to you. The GCM intent service is responsible for delivering data to you and that is it. You will need to use the notification service to show the appropriate notification to the user.
There are advantages / disadvantages to this approach. Application code will run on android when you receive push notifications, which is not the case on the iPhone. You also have the flexibility to silently make updates or notify the user based on the type of push notification.
Register your device with the sender ID when your app starts up and you should receive notifications as expected. All push notifications will be delivered to this method protected void onMessage(Context context, Intent intent)
on the GCMIntentService of your choice.
Try to unregister and register your device again. In DemoActivity.java put
final String regId = GCMRegistrar.getRegistrationId(this);
GCMRegistrar.unregister(this);
then ,remove GCMRegistrar.unregister(this);
in second launch.
Update
Notifications in your Application:
Create class
public class DemoApplication extends Application {
private class NotifyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "RECEIVE MESSAGE", Toast.LENGTH_SHORT).show();
}
}
private NotifyReceiver notifyReceiver = new NotifyReceiver();
@Override
public void onCreate() {
registerReceiver(notifyReceiver, new IntentFilter("GCM_MESSAGE"));
super.onCreate();
}
@Override
public void onTerminate() {
unregisterReceiver(notifyReceiver);
super.onTerminate();
}
}
Then put
<application
android:name=".DemoApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
in AndroidManifest.xml and send broadcast in
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = getString(R.string.gcm_message);
displayMessage(context, message);
context.sendBroadcast(new Intent("GCM_MESSAGE"));
// notifies user
generateNotification(context, message);
}
As alternative case you can register broadcastReceiver in Manifest ,Activity or ForeGround Service
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With