1

I want to notify the user about the messages the she/he received while offline every time the the device is connected to the internet even if the app is closed. I found out that using GCM Network Manager is one way of solving the problem so I followed this tutorial on how to use GCM Network Manager, I added in its sample code a function to display a notification. This function is called everytime the broadcastreceiver is called.

Here is the code

.....
.......
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
    mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String taskId = intent.getStringExtra(CodelabUtil.TASK_ID);
            String status = intent.getStringExtra(CodelabUtil.TASK_STATUS);

            mTaskAdapter.updateTaskItemStatus(taskId, status);
            Notify("TEST", "TEST", context, intent);
        }
    };



private void Notify(String notificationTitle, String notificationMessage, Context ctx, Intent intent){
        Log.i("TEST", "Notify is called");
        NotificationManager manager;
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification myNotication;
        //Intent intent = new Intent("com.google.codelab.networkmanager");

        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1, intent, 0);

        Notification.Builder builder = new Notification.Builder(ctx);

        builder.setAutoCancel(false);
        builder.setContentTitle(notificationTitle);
        builder.setContentText(notificationMessage);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);

//
        //builder.build();

        myNotication = builder.getNotification();
        manager.notify(11, myNotication);

    }

The problem is that the notification only displays when the app is in foreground but does not display whenever the app is closed.

Is there anything that I need to do?. Thank you. (Sorry for my bad english)

ErenRavenHeart
  • 281
  • 3
  • 6
  • 15
  • Implement this on a sticky service to work even if the app is closed. – SripadRaj Jun 15 '16 at 07:51
  • It may also depend on the GCM payload you send from your application server. Make sure to use `data` instead of `notification` in the GCM payload to send data. Check this answer http://stackoverflow.com/a/37067866/3533289 – Much Overflow Jun 15 '16 at 07:57
  • share your manifest and Gcmpushnotification class – Yogesh Rathi Jun 15 '16 at 08:13
  • @SripadRaj Im sorry but I don't really know how to use sticky service on a GCM Network Manager particularly in a GcmTaskService type of service, if it is alright with you can you explain on how to implement sticky service on the mentioned type of service. – ErenRavenHeart Jun 15 '16 at 08:13
  • thank you all for our comments, but what i'm planning to do with my app is to notify the web server every time the device is online so that the server will send its notifications to the device while the device is listening, is that even possible? or is there other way of doing so?, I'm only using GCM Network Manager to notify the server that the device is online but as I have read your comments it seems like there are other way. I will study further on GCM and apply all of your suggestions as soon as possible thank you for your help. – ErenRavenHeart Jun 15 '16 at 08:22
  • have you find any way @ErenRavenHeart – M.Yogeshwaran Oct 26 '16 at 10:30

0 Answers0