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

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

相关内容

热门资讯

第9分钟了解!杭州都莱有没有挂... 第9分钟了解!杭州都莱有没有挂!好像一直都是有辅助工具(确实有挂)-哔哩哔哩1)杭州都莱有没有挂有没...
第六分钟了解!叮叮川南游戏辅助... 第六分钟了解!叮叮川南游戏辅助软件!切实一直都是有辅助攻略(有挂秘籍)-哔哩哔哩1、打开软件启动之后...
两分钟了解!微乐小程序晃晃脚本... 两分钟了解!微乐小程序晃晃脚本!总是存在有辅助方法(有挂工具)-哔哩哔哩一、微乐小程序晃晃脚本可以开...
第九分钟了解!广西八一字牌可以... 第九分钟了解!广西八一字牌可以破解吗!一直一直总是有辅助神器(果真有挂)-哔哩哔哩1、金币登录送、破...
第三分钟了解!星悦游戏辅助器!... 第三分钟了解!星悦游戏辅助器!都是一直总是有辅助工具(果真有挂)-哔哩哔哩1、星悦游戏辅助器有没有辅...
六分钟了解!天天卡五星辅助官网... 六分钟了解!天天卡五星辅助官网入口!本来一直总是有辅助技巧(了解有挂)-哔哩哔哩天天卡五星辅助官网入...
第六分钟了解!新518互娱脚本... 您好,新518互娱脚本下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
五分钟了解!在哪买到科乐辅助器... 五分钟了解!在哪买到科乐辅助器!都是一直都是有辅助神器(发现有挂)-哔哩哔哩1、打开软件启动之后找到...
2分钟了解!闲逸游戏游透视吗!... 2分钟了解!闲逸游戏游透视吗!确实是真的有辅助神器(有挂头条)-哔哩哔哩1、闲逸游戏游透视吗免费脚本...
第九分钟了解!打两圈软件辅助器... 您好,打两圈软件辅助器下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...