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

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

相关内容

热门资讯

教你透视!wepoker代打辅... 教你透视!wepoker代打辅助机器人,微乐家乡官方app,项目教程(有挂透明挂)-哔哩哔哩1、用户...
科普透视!德州局怎么透视,淘宝... 科普透视!德州局怎么透视,淘宝的微乐辅助是真的吗,演示教程(真的有挂)-哔哩哔哩1、这是跨平台的德州...
揭幕透视!德州局透视脚本,微乐... 揭幕透视!德州局透视脚本,微乐小程序辅助开发,资料教程(有挂存在)-哔哩哔哩1、上手简单,内置详细流...
教你透视!德普之星辅助器app... 教你透视!德普之星辅助器app,陕西微乐小程序辅助,窍要教程(有挂工具)-哔哩哔哩1、德普之星辅助器...
了解透视!wepoker科技辅... 了解透视!wepoker科技辅助器,微乐春天扑克开挂辅助插件,绝活教程(有挂方针)-哔哩哔哩1、不需...
辅助透视!werplan免费挂... 辅助透视!werplan免费挂下载,陕西微乐小程序脚本,手段教程(有挂技巧)-哔哩哔哩进入游戏-大厅...
专业透视!wejoker免费脚... 专业透视!wejoker免费脚本,微信小程序微乐家乡辅助器,经验教程(有挂详细)-哔哩哔哩wejok...
总结透视!wepoker数据分... 总结透视!wepoker数据分析工具,微乐自建房辅助软件有哪些功能,方案教程(有挂细节)-哔哩哔哩1...
普及透视!aapoker插件下... 普及透视!aapoker插件下载,微信小程序微乐辅助器下载,技法教程(有挂透视)-哔哩哔哩aapok...
揭幕透视!wepoker网页版... 揭幕透视!wepoker网页版透视方法,微乐自建房辅助可信吗,技法教程(有挂助手)-哔哩哔哩一、we...