小编Pra*_*yap的帖子

FireBase Cloud Messaging Service的onMessageReceived上未显示Toast

我使用以下代码制作Log,Toast并从onMessageReceived发送广播到服务。

我可以在日志中看到使用Firebase控制台发送的数据。但是Toast没有显示,Broadcast也没有发送。

代码如下:

class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Toast.makeText(getApplicationContext(),"From: " + remoteMessage.getFrom(),Toast.LENGTH_SHORT).show();

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            Toast.makeText(getApplicationContext(),"Toast shown",Toast.LENGTH_LONG).show();
            Intent i = new Intent();
            i.setAction("firebase.message.combinedthings");
            sendBroadcast(i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

firebase-cloud-messaging

3
推荐指数
1
解决办法
2335
查看次数

标签 统计

firebase-cloud-messaging ×1