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 类,如下所示:

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

相关内容

热门资讯

总结透视!hardrock透视... 总结透视!hardrock透视工具!本来存在有辅助神器(有挂助手)-哔哩哔哩1、下载好hardroc...
推荐透视!智星菠萝辅助,pok... 推荐透视!智星菠萝辅助,poker红龙辅助,步骤教程(有挂助手)-哔哩哔哩1、起透看视 智星菠萝辅助...
教你透视!拱趴大菠萝万能辅助器... 教你透视!拱趴大菠萝万能辅助器!好像是真的有辅助攻略(揭秘有挂)-哔哩哔哩1、下载好拱趴大菠萝万能辅...
曝光透视!wepoker辅助透... 曝光透视!wepoker辅助透视软件,wpk模拟器多开,教材教程(有挂技巧)-哔哩哔哩1、上手简单,...
总结透视!pokermaste... 总结透视!pokermaster破解版!其实一直总是有辅助软件(有挂方法)-哔哩哔哩所有人都在同一条...
解谜透视!wepoker辅助器... 解谜透视!wepoker辅助器安装包定制,德普之星辅助工具如何设置,策略教程(有挂分享)-哔哩哔哩;...
总结透视!竞技联盟透视!真是是... 总结透视!竞技联盟透视!真是是有辅助技巧(有挂解惑)-哔哩哔哩1、全新机制【竞技联盟透视ai辅助工具...
开挂透视!wpk透视辅助下载,... 开挂透视!wpk透视辅助下载,哈糖大菠萝辅助器,绝活教程(有挂神器)-哔哩哔哩所有人都在同一条线上,...
详细透视!哈糖大菠萝怎么挂!本... 详细透视!哈糖大菠萝怎么挂!本来一直都是有辅助技巧(有挂总结)-哔哩哔哩暗藏猫腻,小编详细说明哈糖大...
专业透视!wepoker软件辅... 专业透视!wepoker软件辅助程序,aapoker脚本怎么用,阶段教程(讲解有挂)-哔哩哔哩1、下...