AndroidStudio在应用程序未运行时无法接收通知负载
创始人
2024-08-20 12:30:04
0

当应用程序未运行时,Android Studio 无法接受推送通知的负载。要解决此问题,您需要在应用程序中实现 FirebaseMessagingService 类以处理通知负载。在 FirebaseMessagingService 类中,您可以使用 onMessageReceived 方法来接收通知负载并执行所需的操作。以下是一个示例:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMessagingService";

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

    // Get notification payload
    String notificationTitle = remoteMessage.getNotification().getTitle();
    String notificationBody = remoteMessage.getNotification().getBody();

    // Get data payload
    String dataValue1 = remoteMessage.getData().get("key1");
    String dataValue2 = remoteMessage.getData().get("key2");

    // Handle notification
    sendNotification(notificationTitle, notificationBody, dataValue1, dataValue2);
}

private void sendNotification(String notificationTitle, String notificationBody, String dataValue1, String dataValue2) {
    // Build notification
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
            .setContentTitle(notificationTitle)
            .setContentText(notificationBody)
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true);

    // Handle data payload
    if (dataValue1 != null && dataValue2 != null) {
        // Do something with data values
    }

    // Show notification
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, notificationBuilder.build());
}

}

您还需要在 AndroidManifest.xml 文件中声明您的 FirebaseMessagingService 类,如下所示:

现在您可以在应用程序未运行时接受推送通知并处理通知负载。

相关内容

热门资讯

十秒钟稳赢!wpk微扑克最新辅... 十秒钟稳赢!wpk微扑克最新辅助(辅助挂)外挂辅助安装(2024已更新)(哔哩哔哩)1、让任何用户在...
透明挂透视(吉祥麻将辅牌器哪里... 透明挂透视(吉祥麻将辅牌器哪里搞)原来真的有挂(透视)详细教程(有挂方法)-哔哩哔哩准备好在吉祥麻将...
研究成果!微扑克用模拟器,we... 研究成果!微扑克用模拟器,wePOKE软件透明挂,详细教程(有挂教程)-哔哩哔哩;一、微扑克用模拟器...
透视辅助!wpk德州辅助,智星... 透视辅助!wpk德州辅助,智星德州辅助,详细教程(有挂透明)-哔哩哔哩;(需添加指定薇1367043...
一瞬间教学!微扑克app确实真... 一瞬间教学!微扑克app确实真的有挂,微扑克专用辅助器,详细教程(有挂动态)-哔哩哔哩;微扑克专用软...
玩家必看秘籍!EV扑克辅助软件... 玩家必看秘籍!EV扑克辅助软件,wEPoke软件透明挂,详细教程(有挂技巧)-哔哩哔哩是一款可以让一...
透明辅助!上饶中至麻将有挂的,... 透明辅助!上饶中至麻将有挂的,poker world外挂,详细教程(有挂介绍)-哔哩哔哩;实战中需综...
透视美元局(拱趴大菠萝有外挂的... 透视美元局(拱趴大菠萝有外挂的)原来真的有挂(透视)详细教程(有挂技巧)-哔哩哔哩;科技详细教程小薇...
三分钟总结(微扑克数据)外挂辅... 三分钟总结(微扑克数据)外挂辅助下载(透视)原来真的有挂(2024已更新)(哔哩哔哩);实战中需综合...
透明攻略!aa扑克平台,wEp... 透明攻略!aa扑克平台,wEpoke软件透明挂,详细教程(有挂介绍)-哔哩哔哩;(需添加指定薇757...