且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序处于后台时推送通知

更新时间:2022-12-27 11:09:53

您正在发送通知消息。详细了解如何处理通知消息此处



当您的应用处于前台时,通知消息将传递给您的 onMessageReceived 回调。您可以决定接收到的邮件应该做些什么,例如:生成并显示通知或与您的应用服务器同步或其他。

后台通知消息根据下游消息请求的notification对象中传递的属性自动生成通知,在这种情况下 onMessageReceived 不会被调用 。如果您查看参考文档对于通知消息,您将看到名为 click_action 的字段,您可以使用它来定义在自动生成的通知被点击时启动的Activity:

在Android上,如果设置了此项,当用户单击通知时,会启动一个匹配意图过滤器
的活动。




如果没有设置,主Activity会启动,这就是您现在看到的。

注意:此行为仅适用于通知消息,如果您仅发送数据消息,则所有发送的消息都将导致 onMessageReceived 被调用。


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.