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免费辅助,wp... 透视总结!wpk免费辅助,wpk辅助软件(透视)总是有透视神器(哔哩哔哩)1、wpk辅助软件公共底牌...
透视阶段!德州局透视脚本免费版... 透视阶段!德州局透视脚本免费版下载手机版,约局吧如何查看是否有挂(透视)好像有挂(哔哩哔哩)1、德州...
透视积累!hhpoker的辅助... 透视积累!hhpoker的辅助是真的吗,hhpoker真的假的(透视)真是真的是有透视器(哔哩哔哩)...
透视策略!wepoker有没有... 透视策略!wepoker有没有机器人,wepoker辅助器安装包(透视)切实有挂(哔哩哔哩)1、超多...
透视积累!hhpoker外开挂... 透视积累!hhpoker外开挂靠谱吗,wepoker软件安装包(透视)确实是真的脚本器(哔哩哔哩)1...
透视指南!hhpoker德州机... 透视指南!hhpoker德州机器人,wpk透视辅助靠谱吗(透视)本来是真的挂(哔哩哔哩)wpk透视辅...
透视秘籍!德普之星有辅助软件吗... 透视秘籍!德普之星有辅助软件吗,德扑圈透视挂(透视)一贯有脚本挂(哔哩哔哩)德普之星有辅助软件吗能透...
透视绝活!wepoker破解工... 透视绝活!wepoker破解工具,pokernow辅助工具(透视)都是有挂(哔哩哔哩)1、poker...
透视教程书!wpk透视辅助,w... 透视教程书!wpk透视辅助,wpk作必弊最怕三个东西(透视)果然是真的透视攻略(哔哩哔哩)1、任何w...
透视资料!aapoker怎么控... 透视资料!aapoker怎么控制牌,拱趴大菠萝自动计算机器人(透视)其实是真的挂(哔哩哔哩)1、玩家...