当应用程序处于后台时,无法看到通知上的操作按钮 [英] Cant see action buttons on notification when app is in background

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

问题描述

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

I have added two actions to the notification i.e. accept and reject. I can see both when the app is in foreground. But I cant see the actions when app is in background.

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

}

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

Also I want to handle the intents from these actions. For this I have created a class which extends broadcast receiver,but how to call this in an activity?

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

        }

}

请帮助。谢谢。.

推荐答案

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

In FCM there are three type of messages you can send

1。通知消息


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

FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

在以下情况下使用通知消息您希望FCM代表您的客户端应用程序显示
通知。

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf.

2.Data消息


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

Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

要在
客户端应用程序上处理消息时,请使用数据消息。

Use data messages when you want to process the messages on your client app.

3。通知和数据


同时具有通知和数据有效载荷的消息,包括背景和
前景。在这种情况下,通知将传递到
设备的系统托盘中,而数据有效载荷将传递到启动器活动意图的额外
中。

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

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

So if you want to handle message on client side better you go with Data Message

在这里您会获得所有详细信息

用于发送数据消息

对于数据消息,您必须使用以下URL致电邮寄服务

For Data Message you have to call post service using following URL

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

标题


Authorization:key =您的服务器ey

Authorization:key=yourserverkey

内容类型:application / json

Content-Type: application/json

有效载荷

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

注意:将 to替换为 registration_ids: [ 1, 2, 3, 4, 5, 6]

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

In Application onMessageReceived you can get data message using remoteMessage.getData()

获取数据消息的详细信息 https://firebase.google.com/docs/cloud-messaging/concept-options

For detail already mentioned this https://firebase.google.com/docs/cloud-messaging/concept-options

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

Here you also check to send notification to Topics using data message https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

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

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