Ich bin auf der Anzeige eine Toast-Nachricht eine Laufzeitausnahme bekommenAnzeige Message von Firebase Mitteilung als Toast
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Ich mag den Nachrichtentext als Toast Meldung anzuzeigen, wenn der Benutzer eine Push-Benachrichtigung von erhält überall in der App.
Irgendwelche Hilfe?
public class MyAppFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
if (AppConfig.SYSTEM_WIDE_DEBUG) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getNotification().getBody());
}
}
private void sendNotification(String messageBody) {
Toast.makeText(getApplicationContext(),messageBody,Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pushnotify);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My APP")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Sieht aus wie Sie bereits 'Toast.makeText haben (...)' jedes Mal, wenn Sie anrufen 'sendNotification()'. Stimmt irgendetwas nicht? –
Die Laufzeitausnahme bekomme ich bei jeder Benachrichtigung. –
Rechts. Ich bin mir nicht sicher, ob das mit dem Toast zu tun hat. Möglicherweise mit dem Kontext. Kannst du den kompletten StackTrace bearbeiten und einfügen? –