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

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

相关内容

热门资讯

五分钟手段!we poker辅... 五分钟手段!we poker辅助器(透视)好像是有辅助透视(哔哩哔哩)1、完成we poker辅助器...
第8分钟辅助!财神十三张福袋多... 您好,财神十三张福袋多少钱这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
第十分钟指南书!wepoker... 第十分钟指南书!wepoker透视脚本免费app(透视)本来是真的有辅助工具(哔哩哔哩)在进入wep...
第九分钟辅助!大唐辅助器,功夫... 第九分钟辅助!大唐辅助器,功夫川麻辅助(好像存在有辅助修改器)-哔哩哔哩第九分钟辅助!大唐辅助器,功...
两分钟手段!wepoker手机... 两分钟手段!wepoker手机助手(透视)好像真的是有辅助神器(哔哩哔哩)1、操作简单,无需wepo...
3分钟脚本!广西老友修改器,四... 3分钟脚本!广西老友修改器,四川游戏家园辅助软件下载(真是存在有辅助修改器)-哔哩哔哩该软件可以轻松...
第七分钟诀窍!hhpoker是... 第七分钟诀窍!hhpoker是正规的吗(透视)果然是有辅助神器(哔哩哔哩);1、打开软件启动之后找到...
第五分钟操作!we-poker... 您好,we-poker软件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
第三分钟插件!蘑菇辅助网,哥哥... 第三分钟插件!蘑菇辅助网,哥哥打大a有没有辅助(一直存在有辅助软件)-哔哩哔哩哥哥打大a有没有辅助能...
2分钟总结!wepoker私人... 2分钟总结!wepoker私人局怎么玩(透视)确实真的有辅助脚本(哔哩哔哩)1、操作简单,无需wep...