当应用程序处于后台时推送通知 [英] Push notification when app is in background

查看:129
本文介绍了当应用程序处于后台时推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我开启开启的应用程式并收到通知时,它会启动我希望它启动的意图。当我收到通知后,关闭应用时,我点击通知,它会打开主要目的。



MyGcmListenerService中的代码 p>

  public class MyGcmListenerService extends GcmListenerService {

private static final String TAG =MyGcmListenerService;
文件fileDir,文件;
字符串文件名,filestring;

/ **
*收到消息时调用。
*
* @param from SenderID of sender。
* @param data包含消息数据作为键/值对的数据包。
*对于密钥集使用data.keySet()。
* /

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

Bundle notification = data.getBundle(notification) ;
String title = notification.getString(title);
字符串naam = notification.getString(naam);
String nfcId = notification.getString(nfcId);
Log.d(TAG,From:+ from);
Log.d(标题,标题:+标题);
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);
}

/ **
*创建并显示包含收到的GCM消息的简单通知。
* /
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 * /,notificationBuilder.build());
}
}

我发送的JSON,请注意,我

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


解决方案

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



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

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


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


< blockquote>

如果没有设置,主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.

这篇关于当应用程序处于后台时推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆