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

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

相关内容

热门资讯

第3分钟了解(来玩德州app)... 第3分钟了解(来玩德州app)外挂透明挂辅助ai(透视)软件教程(2023已更新)(哔哩哔哩);来玩...
第3分钟了解(Wepoke助手... 您好:Wepoke助手这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
第九分钟了解(hm3德州)外挂... 您好:hm3德州这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好...
第1分钟了解(WPK漏洞)软件... 第1分钟了解(WPK漏洞)软件透明挂辅助ai(透视)技巧教程(2021已更新)(哔哩哔哩);WPK漏...
第8分钟了解(Wepoke漏洞... 第8分钟了解(Wepoke漏洞)外挂辅助插件安装安装(透视)详细教程(2025已更新)(哔哩哔哩)是...
四分钟了解(喜游联盟)外挂辅助... 四分钟了解(喜游联盟)外挂辅助插件安装软件(透视)教你攻略(2026已更新)(哔哩哔哩);大家肯定在...
第9分钟了解(clubgg)软... 第9分钟了解(clubgg)软件透明挂辅助科技(透视)详细教程(2024已更新)(哔哩哔哩);clu...
7分钟了解(aa扑克伙牌)外挂... 7分钟了解(aa扑克伙牌)外挂辅助插件安装神器(透视)新2025教程(2023已更新)(哔哩哔哩),...
2分钟了解(wpK)外挂智能a... 2分钟了解(wpK)外挂智能ai辅助软件(透视)必胜教程(2025已更新)(哔哩哔哩);1.wpK ...
第九分钟了解(wEpoke)黑... 第九分钟了解(wEpoke)黑科技透明挂辅助软件(透视)必赢教程(2026已更新)(哔哩哔哩);第九...