0

In my app i am using push notification.it is working fine.but when i remove(clean) the app from the task manager.the push notification is not arriving.what is the problem i don't know.If anyone know how can i resolve this.Please tell me.

This is the method by which i am sending the notification :-

private void sendNotification(String msg) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);


        Intent intent = new Intent(this, MainActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Log.e("message.....", msg);


        if (msg.equals("You Have got New Message")) {

            Log.e("msg occuring..", "intent enter in message...");
            intent.putExtra("KEYMESSAGE", "Message");


        } else {

            Log.e("notification occuring..", "notification occurs.....");
            intent.putExtra("KEYNOTIFICATION", "aman");


        }




 PendingIntent  contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon())
                .setContentTitle("Telepoh")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
        mBuilder.setOngoing(false);

        mBuilder.setAutoCancel(true);


        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }

My problem is Notification is coming properly when the app in the running state.but when the app remove(Clean) from the task manager the notification is not arriving.Please if anyone know what is the problem.then please tell me.

aarav
  • 53
  • 8
  • Possible duplicate of [Android app not receiving Firebase Notification when app is stopped from multi-task tray](http://stackoverflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t) – AL. Oct 01 '16 at 07:45
  • Are you receiving the message through broadcast – Mujammil Ahamed Oct 01 '16 at 07:45
  • @MujammilAhamed Yes ! but when the app in the running state or background state.but when i clean the app from the task manager then notification is not arriving. – aarav Oct 01 '16 at 07:47
  • @AL. i am not getting any answer you suggest the link.thatswhy i am asking. – aarav Oct 01 '16 at 08:20
  • @aarav Okay. But what makes your post not a duplicate? From where I see it, the context is identical. – AL. Oct 01 '16 at 08:35
  • @AL. Yes ! but in need a answer. if you know that then please tell me. – aarav Oct 01 '16 at 08:38

2 Answers2

0

Replace this line with

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

this one

intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES|Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Edited:

As per Android guide lines Once App will be force closed , then all services ,receivers and alarm are destroyed so in that case you will not get any notifications

Mohit Trivedi
  • 699
  • 3
  • 13
  • Nope ! this is not working. when the app clean from the task manager. – aarav Oct 01 '16 at 08:17
  • please can you tell me what is the problem ? i am getting the notification when the app in the running state or background state.but when the app clean from the task manager then i am not getting the notification. – aarav Oct 01 '16 at 09:33
  • @ Mohit Trivedi as per your edited answer. there is no chance to get the notification when app kills from the task manager.then how can the whatsapp is working. in whatsapp when we clean the app from the task manager the notification will come. – aarav Oct 01 '16 at 11:00
  • Ohh Dear Just checked now whatsup even not shows any notification if you forcestop whatsup app in setting -> app – Mohit Trivedi Oct 01 '16 at 11:01
  • I am not talking about the force stop.i jss saying that app swipe from the tab manager.when i swiped the app and go into the setting :>> App the app is not force stopped. the force stopped button is enable. – aarav Oct 01 '16 at 11:04
  • Ohh in that case notification should received perfectly – Mohit Trivedi Oct 01 '16 at 11:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124706/discussion-between-mohit-trivedi-and-aarav). – Mohit Trivedi Oct 01 '16 at 11:06
  • No ! in my case when i swiped the app from the task manager .the notification is not arrived. – aarav Oct 01 '16 at 11:09
0

Push notification is a message pushed from the server to the client. Their must be some service in your app running in the background so that your can always entertain your message.

In my opinion please go with Firebase Cloud Messaging. For complete tutorial and code refer below:

https://firebase.google.com/docs/notifications/

AL.
  • 36,815
  • 10
  • 142
  • 281
Ishant
  • 148
  • 7