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.