0

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.

shubham0703
  • 59
  • 2
  • 12
  • Possible duplicate of [Android GCM (push notification): device doesn't receive notification if application is stopped](http://stackoverflow.com/questions/12073449/android-gcm-push-notification-device-doesnt-receive-notification-if-applicat) – Tom Sabel Apr 12 '16 at 10:48
  • post relevant code – saeed Apr 12 '16 at 10:52
  • Before posting it as a duplicate just try to know what the problem is. I have gone through that link but still it didn't solve my problem. – shubham0703 Apr 12 '16 at 10:53
  • please share your whole project. manifest, receivers, listeners, registration services. – Raghav Satyadev Apr 12 '16 at 10:55
  • the problem might be, your app could be terminated by a internal energy manager. Some Huawei devices for example have an energy manager and you have to enable your app in that settings to be protected. As soon as device goes to sleep, app is getting terminated if you don´t protect it. – Opiatefuchs Apr 12 '16 at 11:01
  • @Opiatefuchs even if the device does not go to sleep and i close the application and then try to send a notification it still does not work – shubham0703 Apr 12 '16 at 11:04
  • I think you have to design you app more in a way like a service to stay alive as long as possible (also Services are getting killed, but only if resources are needed or a task manager kills it). – Opiatefuchs Apr 12 '16 at 11:19
  • @Opiatefuchs Is there any link that you can share?? – shubham0703 Apr 12 '16 at 11:23
  • this link, (second) answer from sahil is worth a try: http://stackoverflow.com/questions/24313539/push-notifications-when-app-is-closed. Also, try something else: go to your settings and open the app manager. search your app and enable "show notificartions". Don´t know if this feature is device dependant, maybe your device can´t do that.... – Opiatefuchs Apr 12 '16 at 11:26

1 Answers1

1

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.

Rijo k Jose
  • 57
  • 1
  • 8