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

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

相关内容

热门资讯

手筋透视!wepokerplu... 手筋透视!wepokerplus脚本,德州透视是真的吗,力荐教程(有挂详细)-哔哩哔哩;亲,德州透视...
盘点透视"wepok... 盘点透视"wepoker透视辅助挂的游戏特色"开挂(透视)辅助工具(信息共享攻略教程)盘点透视"we...
指引透视!智星菠萝有挂吗,we... 指引透视!智星菠萝有挂吗,wepoker插件辅助,可靠技巧(真是有挂)-哔哩哔哩;智星菠萝有挂吗中的...
关于透视"wepok... 关于透视"wepoker有没有透视辅助挂"开挂(透视)辅助软件(重大通报科技教程)关于透视"wepo...
手筋透视!hhpoker有没有... 手筋透视!hhpoker有没有辅助,佛手大菠萝辅助,软件教程(有挂详细)-哔哩哔哩;亲,hhpoke...
教会透视"WePok... 教会透视"WePoker辅助挂教程"开挂(透视)辅助软件(9分钟了解介绍教程)教会透视"WePoke...
步骤透视!wepoker破解游... 步骤透视!wepoker破解游戏盒子,wepoker私人局有透视吗,新2026版(有挂功能)-哔哩哔...
分析透视"wepok... 分析透视"wepoker可以透视开挂吗"开挂(透视)辅助神器(专业讨论教你攻略)分析透视"wepok...
妙招透视!pokermaste... 妙招透视!pokermaster破解版,we poker免费辅助器,科技教程(确实有挂)-哔哩哔哩;...
通报透视"wepok... 通报透视"wepoker是有透视挂的吗"开挂(透视)辅助插件(记者发布扑克教程)通报透视"wepok...