【问题标题】:OneSignal - Get Error after receiving push NotifOneSignal - 收到推送通知后出错
【发布时间】:2017-07-26 05:39:53
【问题描述】:

基于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

收到推送通知后的错误:

致命异常:pool-13-thread-1 进程:com.myapps,PID:24189 java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' 开启 空对象引用 com.mylib.FCMMessageReceiverService.onMessageReceived(FCMMessageReceiverService.java:26) 在 com.google.firebase.messaging.FirebaseMessagingService.zzo(未知 来源)在 com.google.firebase.messaging.FirebaseMessagingService.zzn(未知 来源)在 com.google.firebase.messaging.FirebaseMessagingService.zzm(未知 来源)在 com.google.firebase.iid.zzb$2.run(未知来源)在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 在 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());
    }
}

【问题讨论】:

  • 你检查我的解决方案了吗??
  • 嗨,是的,谢谢。但这里太晚了。如果我有更多问题,我会回复:)

标签: android push-notification onesignal


【解决方案1】:

正如错误提示,

java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String com.google.firebase.messaging.RemoteMessage$Notification.getBody()' 开启 一个空对象引用在

检查名为

的文件

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 请求正文不包含通知对象。

【讨论】:

  • 嗨,我还是不明白如何将通知对象添加到 $message。我已经更新了我的问题。谢谢...
  • 从服务器端更新您从服务器发送的有效负载,以通知有效负载必须具有我建议的正文对象
  • 您在这里不需要 MyFirebaseMessagingService 使用 OneSignalReceivedHandler 来处理收到的任何信号通知。如果您只想处理一个信号通知,甚至不要使用 MyFirebaseMessagingService 。它的库正在处理您将收到通知的所有内容在 OneSignalReceivedHandler 类的 notificationReceived 中。
  • 查看此演示 oneSignal 在 Android 中的工作原理github.com/OneSignal/OneSignal-Android-SDK/blob/master/Examples/…
  • 嗨,我使用android multidexapplication 而不是ExampleApplication 在上面的链接中。所以有什么想法吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2015-12-19
  • 2021-09-28
相关资源
最近更新 更多