0

I'm receiving notifications correctly when the app is on background or closed. But when the app its in foreground, and I send a push notification by firebase, it crashes without no error. Even I receive the object with the notification, I can see it in the console.

My Error code is

08-02 09:17:23.251 13843-27245/com.faraksoch.sagar.cdproutine E/AndroidRuntime: FATAL EXCEPTION: pool-7-thread-1
    Process: com.faraksoch.sagar.cdproutine, PID: 13843
    java.lang.NullPointerException
        at libcore.net.UriCodec.decode(UriCodec.java:153)
        at java.net.URLDecoder.decode(URLDecoder.java:60)
        at com.faraksoch.sagar.cdproutine.fcm.MyFirebaseMessagingService.sendNotification(MyFirebaseMessagingService.java:66)
        at com.faraksoch.sagar.cdproutine.fcm.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:44)
        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(Unknown Source)
        at com.google.firebase.iid.zzc.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:841)

and here is my FirebaseMessagingservice

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyGcmListenerService";

@Override
public void onMessageReceived(RemoteMessage message) {

    String image = message.getNotification().getIcon();
    String title = message.getNotification().getTitle();
    String text = message.getNotification().getBody();
    String sound = message.getNotification().getSound();

    int id = 0;
    Object obj = message.getData().get("id");
    if (obj != null) {
        id = Integer.valueOf(obj.toString());
    }

    this.sendNotification(new NotificationData(image, id, title, text, sound));
}

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param notificationData GCM message received.
 */
private void sendNotification(NotificationData notificationData) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(NotificationData.TEXT, notificationData.getTextMessage());

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = null;
    try {

        notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(URLDecoder.decode(notificationData.getTitle(), "UTF-8"))
                .setContentText(URLDecoder.decode(notificationData.getTextMessage(), "UTF-8"))
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pendingIntent);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    if (notificationBuilder != null) {
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationData.getId(), notificationBuilder.build());
    } else {
        Log.d(TAG, "Não foi possível criar objeto notificationBuilder");
    }
}

}

I'm totally new to Android Studio and don't know how to solve this. Any help would be much appreciated.

  • Please check if the URL you pass to `decode()` at `MyFirebaseMessagingService.java:66` is null. – Ken Y-N Aug 02 '17 at 03:40
  • yes I think I have. I have edit question once again. Can you please suggest me what should I do to remove this error. – Sagar Rawal Aug 02 '17 at 03:56
  • Hi Sagar i think you have to use something else rather than Firebase for push notification of Firebase Cloud Messaging – Shubham Sejpal Aug 02 '17 at 04:19
  • Thnak you for your kind words. I was curious why is that and what alternative do I have in place of Firebase push notification. – Sagar Rawal Aug 02 '17 at 04:21
  • Android Studio has a built-in debugger, and whilst I haven't actually used it, it should give you a stack trace of your error, and when you follow the stack back to `MyFirebaseMessagingService.java:66` you probably arrive at either `notificationData.getTitle()` or `notificationData.getTextMessage()`; check to see if either of these fields are null - code like `notificationData.getTitle() ? notificationData.getTitle() : "NO TITLE"` is a quick hack fix, for instance. – Ken Y-N Aug 02 '17 at 05:31
  • No It is set to "UTF-8" but here is the code that has null i.e NotificationCompat.Builder notificationBuilder = null; Is this line causing the problem? – Sagar Rawal Aug 02 '17 at 08:00

0 Answers0