Display MessageBody of Firebase Notification as Toast
Asked Answered
D

1

6

I am getting a run-time exception on displaying a Toast message

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I want to display the message body as a Toast message when the user receives a push notification from anywhere in the app.

Any help?

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());
    }
}
Deliadelian answered 18/1, 2017 at 20:47 Comment(5)
Looks like you already have Toast.makeText(...) every time you call sendNotification(). Is there something wrong?Costumer
The runtime exception I get on every notification.Deliadelian
Right. I'm not sure that's related to the Toast. Possibly with the context. Can you edit and put in the complete stacktrace?Costumer
I can. But it will be tomorrow when I return to work.Deliadelian
No need. @Coeus.D's answer should suffice. Just use a Handler.Costumer
C
20

If you want show Toast.Run on the UI Thread is the best way;

Example:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    public void run() {
        Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show();
    }
});

Another way:

if (Looper.myLooper() == null) {
    Looper.prepare();
    Toast.makeText(getApplicationContext(), "xxxxxxxxx", Toast.LENGTH_SHORT).show();
    Looper.loop();
}

Because in source code Toast used Handler.But current Thread did't init Looper.

Chronograph answered 19/1, 2017 at 3:42 Comment(1)
Perfect answer. Thank @Coeus.Freaky

© 2022 - 2024 — McMap. All rights reserved.