1

I've followed this tutorial to create a new GCM Listener service: http://www.appifiedtech.net/2015/08/01/android-gcm-push-notification-example/

The code for the listener service:

@Override
public void onMessageReceived(String from, Bundle data) {
    super.onMessageReceived(from, data);
    String msg = data.getString("message");
    Log.d(TAG,"GCM Message received is "+msg);
    // Notifying to user code goes here
    notifyUser(getApplicationContext(),msg);
}

public void notifyUser(Context context,String data){
    Intent intent = new Intent(context, NotificationActivity.class);
    intent.putExtra("data", data);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
            Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setAutoCancel(true);
    builder.setContentTitle("New Notification");
    builder.setContentIntent(pendingIntent);
    builder.setContentText(data);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    builder.setSound(uri);
    notificationManager.notify(countNotification++, builder.build());
    Log.v(TAG,"count "+countNotification);
}

When the app is running (foreground), this works fine and launches the Notification Activity as it should.

However, when it's running in background, I get the notification but the title and body are as defined in my server sender application, and tapping on it takes me to the Main Activity.

  1. This essentially means that when it's running in the background something else handles the notification? Is there another handler I should implement to be able to manage that notification and send the user to the correct activity?

  2. The screen does not wake when I receive this notification, nor does an LED light up on the phone as notifications from other applications do. How do you manage that?

(permissions, services and receiver are defined in the manifest as described in the tutorial)

soc.efs
  • 88
  • 7

2 Answers2

1
  1. In problem regarding when your apps running in the background something else handles the notification?

This SO question can help you in answering your question regarding the GCM Listener Service

  1. In the problem regarding to the screen that doesn't wake when you receive notification.

Use ACQUIRE_CAUSES_WAKEUP

Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

You can visit this SO question in how to use it.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Thanks KENdi, I figured out these two myself, however I still cannot get the LED to light up on GCMReceiver. `builder.setLights(Color.BLUE,500,500);` Any ideas on that? – soc.efs Mar 10 '16 at 06:12
  • Got LEDs to work as well. For some reason BLUE was not working while GREEN was fine. In short, for my question, you need to make a nearly identical to GCMListenerService implementation of GCMReceiver. To wake the processor, you need to use PowerManager newWakeLock and Acquire/Release. – soc.efs Mar 11 '16 at 13:43
0

You remove

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);

Using intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

In the NotificationActivity class , you need implement the onNewIntent() callback when you open NotificationActivity existed the stack.

Thanh Thinh
  • 120
  • 1
  • 4
  • Using FLAG_ACTIVITY_SINGLE_TOP made no difference. I also put an override of onNewIntent() in NotificationActivity with no results, same as before. When application is on background, only MainActivity is opened when I tap the notification – soc.efs Mar 07 '16 at 16:28