android - 应用程序在后台时推送通知

标签 android json google-cloud-messaging

我在我的 Android 应用程序中实现了 Google Cloud Messaging。当我在打开应用程序时发送带有 JSON 的消息时,它的行为与关闭应用程序时不同。

当我打开 应用程序并收到通知时,它会启动我希望它启动的 Intent 。当我收到通知时关闭应用程序,然后单击通知,它会打开主要 Intent 。当我关闭我的应用程序并收到推送通知时,我该如何说明它需要打开哪个 Intent?

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

我发送的 JSON,请注意我故意遗漏了 token_mobile

{
    "to": "TOKEN_MOBILE****************",
    "notification": {
        "title": "Nieuwe overledene",
        "body": "Henk",
        "naam": "Henk",
        "nfcId": "80-40-A3-4A-C2-D6-04"
    }
}

最佳答案

您正在发送通知消息。查看有关如何处理通知消息的更多信息 here .

当您的应用程序在前台时,通知消息会传递到您的 onMessageReceived打回来。在那里您可以决定应该如何处理收到的消息,例如:生成并显示通知或与您的应用服务器同步或其他。

当您的应用程序处于后台时,通知消息会根据下游消息请求的“通知”对象中传递的属性自动生成通知,onMessageReceived 在这种情况下不被调用。如果您查看 reference docs对于通知消息,您将看到一个名为 click_action 的字段您可以使用它来定义在点击自动生成的通知时启动的 Activity:

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

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

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

关于android - 应用程序在后台时推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37542149/

相关文章:

Android - ClassCastException - 在从 LinearLayout 到 AbsListView 的 LayoutParams

java - 将值添加到 recyclerView 的部分中

android - 对于 Android 和 iOS 使用相同的包标识符有什么缺点吗?

php - 在 Objective C 中从 URL 下载 JSON 的简单方法

android - GCM 服务器以什么频率刷新注册 ID 以及如何在我的手机中获取 regId 更改事件?

php - GCM 向多个客户端发送多条消息(数组)

android - 如何获得带小数点的电池温度?

json - 如果名称中没有 [],ExtJS comboBox 仅从 JSON 设置值

javascript - 在javascript中从json中获取值

android - GCM 未通过广播接收器传送