【问题标题】:Firebase Push Notifications does not receiveFirebase 推送通知未收到
【发布时间】:2018-01-31 10:56:11
【问题描述】:

我正在尝试 Firebase 推送通知。我做了教程中所说的一切,但它不起作用。

FirebaseMessagingService:

  package com.example.firebasenf.firebasenf;

import com.google.firebase.messaging.FirebaseMessagingService;/

public class MyFirebaseMessagingService extends FirebaseMessagingService {
}

FirebaseInstanceIdService:

package com.example.firebasenf.firebasenf;

import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
}

清单:

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

【问题讨论】:

  • 至少创建一个通知方法来发送通知..它不会发生魔法
  • 这是你的完整代码吗??
  • 是的,但我做了和我在这里看到的一样的事情:youtube.com/watch?v=XZtDZyW4Zvc

标签: android firebase firebase-notifications


【解决方案1】:

您需要在代码中添加一些方法:

FirebaseMessagingService:

    package com.example.firebasenf.firebasenf;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage){
        Intent intent = new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("Application Title");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
}

FirebaseInstanceIdService :

package com.example.firebasenf.firebasenf;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String REG_TOKEN = "REG_TOKEN";

    @Override
    public void onTokenRefresh(){
        String recent_token = FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN,recent_token);
    }
}

【讨论】:

    【解决方案2】:

    您需要在MyFirebaseMessagingService 类中覆盖onMessageReceived,以便在推送通知时执行一些操作。

    doc

    通过重写 FirebaseMessagingService.onMessageReceived 方法, 您可以根据接收到的 RemoteMessage 对象执行操作,并且 获取消息数据

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 
    {
    
        Log.d(TAG, "From: " + "Notification Received");
    
    
        // TODO(developer): Handle FCM messages here.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
    
        // Check if message contains a data payload.
        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());
        }
    
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    
        //You may want show notification
    }
    

    记住onMessageReceived只有在应用程序在前台时才会触发

    当您的应用处于后台时,Android 会发送通知 消息到系统托盘。用户点击通知会打开 默认应用启动器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 2020-08-02
      • 2020-02-17
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多