应用程序关闭android时,firebase通知未显示在snackbar中

时间:2016-11-27 19:16:27

标签: android firebase-cloud-messaging

我想在 FirebaseMessagingService 中通过广播接收器在活动小吃栏中显示 firebase推送通知(发送firebase控制台),但我无法显示。请帮帮我。

清单:

<service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

firebase消息服务:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if(remoteMessage.getData().size()>0){
            Log.v("Message data", remoteMessage.getData().toString());
        }

        if(remoteMessage.getNotification()!=null){
            Log.v("MessageNotification", remoteMessage.getNotification().getBody().toString());
        }

        if(remoteMessage!=null){
            sendNotification(remoteMessage.toString());
        }
    }

    private void sendNotification(String messageBody){

        Intent intent=new Intent(this,NotificationShowActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("FireBaseNotification",messageBody);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.notification_icon)
                .setContentTitle("Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

notificationShow活动:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_show);

        linearLayout=(RelativeLayout)findViewById(R.id.activity_notification_show);

    }


    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(broadcastReceiver,new IntentFilter(MyFirebaseMessagingService.NOTIFICATION_SERVICE));

    }

    //broadcastReceiver

    private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
        @Override

        public void onReceive(Context context, Intent intent) {

            String message=intent.getStringExtra("FireBaseNotification");
            if(message!=null){

                Toast.makeText(NotificationShowActivity.this,message,Toast.LENGTH_LONG).show();

            }
        }
    };

}

1 个答案:

答案 0 :(得分:0)

要在后台捕获FCM消​​息,您应该使用接收器,而不是服务。

public class NotificationsReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //handle your data
        abortBroadcast(); //stop sending this broadcast to other receivers.
    }
}

在你的清单中(重要 - 将优先级设置为999,成为接收队列中的第一个):

<receiver
        android:name=".fcm.NotificationsReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter android:priority="999">
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>