Reputation: 59
I have been implementing Push Notification in one of my application and it is working fine till the time app is open or it is running in background.
But after removing the application from the background I am unable to receive notification on my device. I have gone through almost every link related to this problem and have most probably implemented everything that is required ti receive a notification.
I am just stuck on this point only so if there is anyone who knows how to receive notification when the app is closed please do post a solution.
Your help will be highly appreciated.
Service Class Code:
public void onMessageReceived(String from, Bundle data){
String message = data.getString("message");
//createNotification(mTitle, push_msg);
Log.e("*Notification-Response*" , from);
Log.e("*Notification-Bundle*" , String.valueOf(data));
//checkApp();
generateNotification(getApplicationContext(),message);
}
private static void generateNotification(Context context, String message) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Dashboard.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.appicon)
.setContentTitle("Title")
.setContentText(message)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(999999, notification);
}
Manifest:
<service
android:name=".GCM.PushNotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.google.android.gcm.demo" />
</intent-filter>
</receiver>
Thanks.
Upvotes: 0
Views: 5119
Reputation: 57
May be the problem is with registering the deviceID not properly. Ondestroy() may be calling. Thus deviceID will also get removed while removing app from background. So need to register deviceID again.
Upvotes: 1