【问题标题】:firebase notification not showing in snackbar while app closed android应用程序关闭时,firebase 通知未显示在小吃栏中
【发布时间】:2016-11-28 04:40:12
【问题描述】:

我想通过广播接收器在活动的快餐栏中显示 firebase 推送通知(发送 firebase 控制台),这是我从 FirebaseMessagingService 获得的,但我无法显示。请帮助我.

清单:

<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();

            }
        }
    };

}

【问题讨论】:

  • 你如何发送数据,是使用 Firebase 控制台还是你已经实现了自己的服务器?它是否显示在您的日志语句中?
  • 您的 Android 监视器中是否显示 Log 语句?
  • 是........,,,,
  • 实现自己的服务器

标签: android firebase-cloud-messaging


【解决方案1】:

要在后台捕获 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多