简体   繁体   中英

Push notification when app is in background

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"
    }
}

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.

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM