Firebase推送通知Toast消息致命异常

时间:2018-06-28 14:29:12

标签: android firebase crash firebase-cloud-messaging

我从Crashlytics获得了一个奇怪的崩溃报告,说我的应用程序崩溃是因为UI thread中没有调用Toast消息。这很奇怪,因为我没有显示来自推送通知的Toast消息。看起来它正在为成千上万的不同用户工作,但是为此崩溃了。我不知道这是怎么回事。以下是报告:

Fatal Exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
       at android.os.Handler.(Handler.java:209)
       at android.os.Handler.(Handler.java:123)
       at android.widget.Toast$TN.(Toast.java:350)
       at android.widget.Toast.(Toast.java:106)
       at android.widget.Toast.makeText(Toast.java:264)
       at android.media.RingtoneManager.isRingtoneExist(RingtoneManager.java:1195)
       at android.app.NotificationManager.notify(NotificationManager.java:235)
       at com.google.firebase.messaging.zza.zzt(Unknown Source)
       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:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)

应用依赖项:

implementation 'com.google.firebase:firebase-core:12.0.1'
implementation 'com.google.firebase:firebase-messaging:12.0.1'

设备:Android 6.0-阿尔卡特Shine Lite 5080X

firebase推送服务:

public class FirebasePushService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(App.NAME, "message received");

        Map<String, String> map = remoteMessage.getData();
        JSONObject json = new JSONObject();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            json.put(key, value);
        }

        int pushType = json.getInt("push_type", -1);

        if (pushType > 0) {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            PendingIntent pi = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_ONE_SHOT);

            createNotification();
        }
    }

    private void createNotification() {
        // create the channel first
        createChannels();

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Build b = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSound(defaultSoundUri)
                .setColor(ContextCompat.getColor(context, R.color.primaryColor));

        b.setContentIntent(pi);
        b.setAutoCancel(true);
        Notification not = b.build();

        not.flags |= Notification.FLAG_AUTO_CANCEL;

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            not.ledARGB = Color.GREEN;
            not.flags = Notification.FLAG_SHOW_LIGHTS;
            not.ledOnMS = 1000;
            not.ledOffMS = 1000;
            not.defaults |= Notification.DEFAULT_VIBRATE;
            not.defaults |= Notification.DEFAULT_SOUND;
        }

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(PUSH_NOTIFICATION_ID, not);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChannels() {

        // create android channel
        NotificationChannel androidChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        androidChannel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        androidChannel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        androidChannel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        getManager().createNotificationChannel(androidChannel);
    }
}

有人知道发生了什么吗?

0 个答案:

没有答案