推送通知 - 应用程序处于前台时应用程序崩溃

时间:2017-08-02 03:34:22

标签: java android eclipse firebase nullpointerexception

我在应用处于后台或关闭时正确接收通知。但当应用程序处于前台时,我发送了firebase的推送通知,它崩溃而没有错误。即使我收到带有通知的对象,我也可以在控制台中看到它。

我的错误代码是

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)

这是我的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");
    }
}

}

我是Android Studio的新手,并且不知道如何解决这个问题。任何帮助将不胜感激。

0 个答案:

没有答案