当应用在后台

时间:2017-06-11 12:58:47

标签: java android push-notification action

我在通知中添加了两个操作,即接受和拒绝。 当应用程序处于前台时,我可以看到两者。但我无法看到应用程序处于后台时的操作。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    private String mOrderId, mBillId;
    private Boolean mUpdateNotification;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);


        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.describeContents());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        //get data from server notification.


        sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
    }

    //send notification

    private void sendNotification(String messageBody, String title) {

        Intent intent = new Intent();

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        long[] pattern = {500, 500, 500, 500, 500};
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.login_logo_1)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setVibrate(pattern)
                .setSound(defaultSoundUri)
                .addAction(R.string.accept,getString(R.string.accept), pendingIntent)
                .addAction(R.string.reject,getString(R.string.reject), pendingIntent)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 , notificationBuilder.build());
    }

}

此外,我想处理这些操作的意图。为此,我创建了一个扩展广播接收器的类,但是如何在活动中调用它?

public class NotificationReceiver extends BroadcastReceiver {

    String ACCEPT,REJECT;

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(ACCEPT.equals(action)) {
                Log.v("Delivery","Accepted");
            }
            else if(REJECT.equals(action)) {
                Log.v("Delivery","Rejected");
            }

        }

}

请帮忙。谢谢..

1 个答案:

答案 0 :(得分:1)

在FCM中,您可以发送三种类型的消息

<强> 1。通知消息

  

FCM代表自动向最终用户设备显示消息   客户端应用程序。通知消息具有预定义的一组   用户可见键和自定义键值的可选数据有效内容   对

     

当您希望FCM处理显示时,请使用通知消息   代表您的客户端应用程序发送通知。

2.数据讯息

  

客户端应用程序负责处理数据消息。数据消息   只有自定义键值对。

     

如果要处理数据消息,请使用数据消息   客户端应用。

第3。通知和数据

  

包含通知和数据有效负载的消息,包括背景和   前景。在这种情况下,通知将传递给   设备的系统托盘,数据有效负载在附加功能中提供   您的启动器活动的意图。

enter image description here

因此,如果您想更好地处理客户端上的消息,请使用数据消息

here you get all details

用于发送数据消息

对于数据讯息,您必须使用以下网址

来呼叫邮政服务

https://fcm.googleapis.com/fcm/send

<强>接头

  

授权:键= yourserverkey

     

Content-Type:application / json

<强>有效载荷

{"data": "extra data to be send",
 "to" : "devicetoken"
}

注意:将“to”替换为“registration_ids”:[“1”,“2”,“3”,“4”,“5”,“6”]用于多个设备 < / p>

在应用 onMessageReceived 中,您可以使用 remoteMessage.getData()

获取数据消息

有关详细信息,请参阅此https://firebase.google.com/docs/cloud-messaging/concept-options

您还可以在此检查使用数据消息https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

向主题发送通知
相关问题