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有那种辅助吗(透视)微乐a3纸牌有脚本(其实真的是有辅助脚本)-哔哩哔哩1.wpk有那种...
今年以来!黑侠破解wepoke... 今年以来!黑侠破解wepoker(透视)微乐麻将脚本透视(一直是真的辅助方法)-哔哩哔哩1、黑侠破解...
长期以来!wepoker有辅助... 长期以来!wepoker有辅助插件吗(透视)微乐广西小程序开发(一贯是有辅助插件)-哔哩哔哩亲,关键...
透视软件!wepoker有辅助... 透视软件!wepoker有辅助工具吗(透视)微信小程序微乐房间透视(都是存在有辅助挂)-哔哩哔哩1、...
透视实锤!cloudpoker... 透视实锤!cloudpoker怎么开挂(透视)手机微乐小程序破解器(一贯真的是有辅助攻略)-哔哩哔哩...
有了最新消息!拱趴大菠萝开挂方... 有了最新消息!拱趴大菠萝开挂方法(透视)微乐云南小程序修改器ios(本来是真的辅助软件)-哔哩哔哩1...
据文件显示!wepoker有什... 据文件显示!wepoker有什么规律(透视)微乐小程序辅助工具免费版(一贯是真的辅助神器)-哔哩哔哩...
透视脚本!wepoker怎么破... 透视脚本!wepoker怎么破解游戏(透视)提高微乐运气的方法(总是是真的辅助攻略)-哔哩哔哩1、许...
透视辅助!如何判断wpk辅助软... 透视辅助!如何判断wpk辅助软件的真假(透视)微乐广西小程序开挂(一贯真的有辅助器)-哔哩哔哩如何判...
更值得关注的是!wepoker... 更值得关注的是!wepoker破解游戏盒子(透视)微乐智能插件激活码(切实是有辅助攻略)-哔哩哔哩w...