OneSignal - 收到推送Notif

时间:2017-03-06 11:14:13

标签: android push-notification onesignal

基于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进程:com.myapps,PID:24189   java.lang.NullPointerException:尝试调用虚方法   “java.lang.String中   com.google.firebase.messaging.RemoteMessage $ Notification.getBody()'on   一个空对象引用   com.mylib.FCMMessageReceiverService.onMessageReceived(FCMMessageReceiverService.java:26)   在com.google.firebase.messaging.FirebaseMessagingService.zzo(未知   来源)at   com.google.firebase.messaging.FirebaseMessagingService.zzn(未知   来源)at   com.google.firebase.messaging.FirebaseMessagingService.zzm(未知   来自)com.google.firebase.iid.zzb $ 2.run(未知来源)at   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());
    }
}

1 个答案:

答案 0 :(得分:2)

如错误所示,

  

java.lang.NullPointerException:尝试调用虚方法   “java.lang.String中   com.google.firebase.messaging.RemoteMessage $ Notification.getBody()'on   

的空对象引用

检查名为

的文件
  

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