3

I implemented Google Cloud Messaging in my Android app. When I send a message with JSON while I have opened the app, it acts different than when I have the app closed.

When I have the app open, and receive the notification, it starts the intent I want it to start. When I have the app closed when i receive the notification, and I click on the notification, it opens the Main intent. How can I say which intent it needs to open when I have closed my app and receive the push notification?

The code in MyGcmListenerService

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";
    File fileDir, file;
    String filename, filestring;

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */

    @Override
    public void onMessageReceived(String from, Bundle data) {

        Bundle notification = data.getBundle("notification");
        String title = notification.getString("title");
        String naam = notification.getString("naam");
        String nfcId = notification.getString("nfcId");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Title: " + title);
        Log.d(TAG, "Naam: " + naam);
        Log.d(TAG, "nfcId: " + nfcId);

        if (from.startsWith("/topics/")) {

        } else {
            filename = "gegevensOverledene";
            ReadWriteFile readWriteFile = new ReadWriteFile(getApplicationContext());
            readWriteFile.writeFileOverledene(filename, naam);
        }

        sendNotification(title, naam, nfcId);
    }

    /**
     * Create and show a simple notification containing the received GCM message.
     */
    private void sendNotification(String title, String naam, String nfcId) {
        Intent intent = new Intent(this, PinLoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("naam", naam);
        intent.putExtra("nfcId",nfcId);
        intent.putExtra("Class",NieuweOverledeneActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(naam)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

        NotificationManager notificationManager =                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

The JSON I send, note that I left out the token_mobile on purpose

{
    "to": "TOKEN_MOBILE****************",
    "notification": {
        "title": "Nieuwe overledene",
        "body": "Henk",
        "naam": "Henk",
        "nfcId": "80-40-A3-4A-C2-D6-04"
    }
}
willemjan92
  • 57
  • 1
  • 11
  • it is in your sendNotification method, you need to set the right intent to PendingIntent you start – Viktor Yakunin May 31 '16 at 09:41
  • It doesn't matter which intent I set in sendNotification, when I receive a message when the app is closed, it **always** starts the Main intent – willemjan92 May 31 '16 at 09:54
  • 1
    This is strange - are you perhaps doing any checks in your `PinLoginActivity` that perhaps may be "redirecting" the user to the Main Activity? Please show us the `PinLoginActivity` code. Also, try changing your `Intent` Flag to `intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);` – ishmaelMakitla May 31 '16 at 10:06
  • `PinLoginActivity` is my Main Activity. In there I have a methode to redirect to a specific class I say with `intent.putExtra("Class",NieuweOverledeneActivity.class);` and `nextScreen = getIntent().getSerializableExtra("Class");` & `Intent nextScreenIntent = new Intent(this,(Class) nextScreen);`. But because `sendNotification` isn't called when the app is closed, it won't have a nextScreen serializable – willemjan92 May 31 '16 at 10:18

2 Answers2

3

You are sending a notification message. See more on how notification messages are to be handled here.

When your app is in the foreground notification messages are passed to your onMessageReceived callback. There you can decide what should be done with the received message, eg: generate and display a notification or sync with your app server or whatever.

When your app is in the background notification messages automatically generate notifications based on the properties passed in the "notification" object of your downstream message request, onMessageReceived is not called in this case. If you look at the reference docs for notification messages you will see a field named click_action you can use this to define what Activity is launched when the automatically generated notification is tapped:

On Android, if this is set, an activity with a matching intent filter is launched when user clicks the notification.

If this is not set the main Activity is launched, which is what you are seeing now.

Note: This behaviour is only for Notification Messages, if you send data only messages then all sent messages will result in onMessageReceived being called.

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
  • How to handle the notification messages with app is in background? – Sanoop Surendran Jan 05 '17 at 09:16
  • Notification messages are handled by the client library when the app is in the background resulting in a displayed notification. If that notification message had an accompanying data payload it will be available in the extras of the intent launched as a result of the user tapping on the notification. – Arthur Thompson Jan 10 '17 at 21:44
  • So basically if an action associated with the notification should be accompanied data payload or notification payload.. Is my understanding correct? – Sanoop Surendran Jan 11 '17 at 04:26
  • The action that is supported is the user tapping on the notification. If a data payload was sent with the notification message then it will be available in the extras of the intent that is launched as a result of the user tapping on the notification. – Arthur Thompson Jan 11 '17 at 21:13
  • Thanks a lot, Definitely helped clearing a lot of doubts :) :) – Sanoop Surendran Jan 12 '17 at 11:51
  • This is working as explained, but how to clear this intent extras when the activity gets destroyed or is stopped? – Kaveesh Kanwal Feb 01 '18 at 09:09
0

check Following

/**
 * Create and show a simple notification containing the received GCM message.
 */
private void sendNotification(String title, String naam, String nfcId) {

    //Create the intent according to your condition and pass it to pendingintent.

    Intent intent = new Intent(this, PinLoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("naam", naam);
    intent.putExtra("nfcId",nfcId);
    intent.putExtra("Class",NieuweOverledeneActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(title)
        .setContentText(naam)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

    NotificationManager notificationManager =                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
mdDroid
  • 3,135
  • 2
  • 22
  • 34
  • It doesn't matter which intent I set in sendNotification, when I receive a message when the app is closed, it **always** starts the Main intent. sendNotification is only called when the app is opened when I receive a notification – willemjan92 May 31 '16 at 09:56