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

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

相关内容

热门资讯

科技揭秘辅助器!天津麻将ai辅... 您好:天津麻将这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...
如何分辨真伪辅助器!皮皮云南麻... 如何分辨真伪辅助器!皮皮云南麻将ai辅助器多功能插件,果然存在有辅助器插件模板主打赛后复盘的 AI ...
分享认知透视!微乐三丁拐ai透... 分享认知透视!微乐三丁拐ai透视多功能插件,其实是真的有透视插件指引主打本地运行的 AI 黑科技工具...
实测发现开挂!锦州麻将ai开挂... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
揭秘攻略透视!皮皮四川麻将至尊... 揭秘攻略透视!皮皮四川麻将至尊版ai透视多功能插件,起初真的是有透视方法指南;近期一款 AI 黑科技...
每日必看推荐辅助!友友麻将ai... 每日必看推荐辅助!友友麻将ai辅助多功能插件,都是真的是有辅助技巧课程是一款可以让一直输的玩家,快速...
玩家实测开挂!诸暨翻财神ai开... 玩家实测开挂!诸暨翻财神ai开挂多功能插件,真是有开挂教程经验是一款可以让一直输的玩家,快速成为一个...
记者发布透视!多多休闲ai透视... 您好,记者发布透视!多多休闲ai透视多功能插件,从来是有透视安装要领这款游戏可以开挂的,确实是有挂的...
推荐十款!透视挂!友乐广西麻将... 推荐十款!透视挂!友乐广西麻将ai透视挂多功能插件,都是真的有透视挂平台指引是一款可以让一直输的玩家...
实测揭晓!透视!渝都麻将ai透... 您好:渝都麻将这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...