questionasker
questionasker

Reputation: 2697

OneSignal - Get Error after receiving push Notif

Based on this guide, I try to integrate my existing Android apps with OneSignal. I put OneSignal code in MainActivity.java :

protected void onCreate(Bundle pSavedInstanceState) 
{

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

}  

Of course, i have created the class handler for .setNotificationOpenedHandler and .setNotificationReceivedHandler The class handler area (in 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);
    }
}

But after getting push Notification, my apps close suddenly and showing error : java.lang.NullPointerException: Attempt to invoke virtual method

The Error i got after receiving Push Notification :

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)

any Idea ?

Many Thanks before...

===Update

My MyFirebaseMessagingService.java class :

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());
    }
}

Upvotes: 1

Views: 2144

Answers (1)

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 4353

As the error suggest ,

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

Check the file named

MyFirebaseMessagingService

within it you will have this function

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());
} 

Add a notification object on your $message. The body of your POST request must be something like:

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

Your remoteMessage.getNotification() returnsnull because the body of your POST request doesn't contain a notification object.

Upvotes: 2

Related Questions