应用在后台时推送通知

3
我在我的Android应用程序中实现了Google Cloud Messaging。当我在打开应用程序时使用JSON发送消息时,与关闭应用程序时的操作不同。
当我应用程序打开并收到通知时,它会启动我想要启动的意图。当我应用程序关闭并且接收到通知时,我单击通知时将打开主意图。如何确定在关闭我的应用程序并接收推送通知时需要打开哪个意图?
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());
    }
}

我发送的JSON,注意我故意省略了token_mobile。
{
    "to": "TOKEN_MOBILE****************",
    "notification": {
        "title": "Nieuwe overledene",
        "body": "Henk",
        "naam": "Henk",
        "nfcId": "80-40-A3-4A-C2-D6-04"
    }
}

在你的sendNotification方法中,你需要为启动PendingIntent设置正确的意图。 - Viktor Yakunin
无论我在sendNotification中设置哪个意图,当我在应用程序关闭时接收到消息时,它总是启动Main意图。 - willemjan92
1
这很奇怪 - 你在 PinLoginActivity 中可能进行了任何检查,可能会将用户“重定向”到主活动吗?请向我们展示 PinLoginActivity 代码。另外,请尝试将您的 Intent 标志更改为 intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); - ishmaelMakitla
PinLoginActivity 是我的主 Activity。在那里,我有一个方法来重定向到一个特定的类,我使用 intent.putExtra("Class",NieuweOverledeneActivity.class);nextScreen = getIntent().getSerializableExtra("Class"); & Intent nextScreenIntent = new Intent(this,(Class) nextScreen);。但是因为当应用程序关闭时 sendNotification 没有被调用,它将没有下一个屏幕可序列化。 - willemjan92
2个回答

3
您正在发送通知消息。有关如何处理通知消息的更多信息,请单击此处
当您的应用程序在前台时,通知消息将传递给onMessageReceived回调函数。在那里,您可以决定对接收到的消息采取什么操作,例如:生成和显示通知或与您的应用程序服务器同步等。
当您的应用程序在后台时,通知消息会根据下行消息请求的“notification”对象中传递的属性自动生成通知,在这种情况下不会调用onMessageReceived。如果您查看通知消息的参考文档,您将看到一个名为click_action的字段,您可以使用它来定义在点击自动生成的通知时启动哪个Activity:

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

如果未设置此选项,则会启动主Activity,这就是您现在看到的内容。 注意:此行为仅适用于通知消息,如果您发送数据消息,则所有发送的消息都将导致调用onMessageReceived

如何在应用程序后台处理通知消息? - Sanoop Surendran
当应用程序在后台时,通知消息由客户端库处理,从而产生一个显示的通知。如果该通知消息有相应的数据有效载荷,则在用户点击通知后启动的意图的附加项中将其可用。 - Arthur Thompson
所以基本上,如果与通知相关联的操作应该伴随着数据有效载荷或通知有效载荷...我的理解是正确的吗? - Sanoop Surendran
支持的操作是用户点击通知。如果通知消息附带了数据有效载荷,则该有效载荷将在启动由用户点击通知而产生的意图的 extras 中可用。 - Arthur Thompson
非常感谢,肯定帮助消除了很多疑虑 :) :) - Sanoop Surendran
这个功能按照说明正常工作,但是当活动被销毁或停止时,如何清除这些意图附加项呢? - Kaveesh Kanwal

0

请检查以下内容

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

无论我在sendNotification中设置哪个意图,当我在应用关闭时接收到消息时,它总是启动Main意图。只有当我接收到通知并打开应用程序时,才会调用sendNotification。 - willemjan92

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接