gpt4 book ai didi

android - OneSignal - 收到推送通知后出错

转载 作者:行者123 更新时间:2023-11-29 19:20:27 34 4
gpt4 key购买 nike

基于 this guide ,我尝试将我现有的 Android 应用程序与 OneSignal 集成。我将 OneSignal 代码放在 MainActivity.java 中:

protected void onCreate(Bundle pSavedInstanceState) 
{

OneSignal.startInit(this)
.setNotificationOpenedHandler(new OneSignalNotificationOpenedHandler())
.setNotificationReceivedHandler(new OneSignalReceivedHandler())
.init();

}

当然,我已经为 .setNotificationOpenedHandler.setNotificationReceivedHandler 创建了类处理程序类处理程序区域(在 MainActivity.java 中):

private class OneSignalReceivedHandler implements OneSignal.NotificationReceivedHandler
{
@Override
public void notificationReceived(OSNotification notification) {
JSONObject data = notification.payload.additionalData;
String customKey;

Log.d("onesignal=","onesignal receiver");
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}
}
}

private class OneSignalNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler
{

@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;

Log.d("onesignal=","onesignal open handler");

if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null)
Log.i("OneSignalExample", "customkey set with value: " + customKey);
}

if (actionType == OSNotificationAction.ActionType.ActionTaken)
Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
}
}

但是在收到推送通知后,我的应用程序突然关闭并显示错误:java.lang.NullPointerException: Attempt to invoke virtual method

收到推送通知后出现的错误:

FATAL EXCEPTION: pool-13-thread-1 Process: com.myapps, PID: 24189 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference at com.mylib.FCMMessageReceiverService.onMessageReceived(FCMMessageReceiverService.java:26) at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source) at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source) at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source) at com.google.firebase.iid.zzb$2.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

有什么想法吗?

非常感谢...

===更新

我的 MyFirebaseMessagingService.java 类:

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//super.onMessageReceived(remoteMessage);
Intent intent = new Intent(this, GameActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notifictionBuilder = new NotificationCompat.Builder(this);
notifictionBuilder.setContentTitle("My Application");

notifictionBuilder.setContentText(remoteMessage.getNotification().getBody());

notifictionBuilder.setAutoCancel(true);
notifictionBuilder.setSmallIcon(R.drawable.ic_launcher);
notifictionBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notifictionBuilder.build());
}
}

最佳答案

如错误提示,

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' on a null object reference at

检查名为

的文件

MyFirebaseMessagingService

在里面你会有这个功能

public class MyFirebaseMessagingService  extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

sendNotification(remoteMessage.getNotification().getBody());
}

在您的 $message 上添加一个通知对象。您的 POST 请求的正文必须类似于:

{
"to" : "aUniqueKey",
"notification" : {
"body" : "Blah Blah!",
"title" : "Hey vs. Hello"
},
"data" : {
"Nick" : "Amigos",
"Room" : "Lasta vs Avista"
}
}

您的 remoteMessage.getNotification() 返回null,因为您的 POST 请求正文不包含通知对象。

关于android - OneSignal - 收到推送通知后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42624154/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com