【问题标题】:Firebase Notifications doesn't come on my deviceFirebase 通知没有出现在我的设备上
【发布时间】:2016-11-07 12:10:32
【问题描述】:

我按照 Google 文档中的说明实施了 Firebase。我从 Firebase 控制台发送消息。但通知不会出现在我的设备上。你能帮助我吗?提前谢谢你。

在我的`AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<permission
    android:name="android.permission.INTERACT_ACROSS_USERS_FULL"
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".another.IntroActivity"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

在MyFirebaseInstanceIdService.java

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "FBIIS";

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

}
}

在MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private String TAG = "MFMS";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Map<String, String> newMsg = remoteMessage.getData();

    Set<String> keys = newMsg.keySet();
    Iterator<String> iterator = keys.iterator();

    while (iterator.hasNext()) {
        String key = iterator.next();

        Log.e(TAG, "key = " + key + " value = " + newMsg.get(key));
    }


    Log.d(TAG, "From: " + remoteMessage.getFrom());


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


    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
}
}

我的应用没有进入 onMessageReceived 函数。不知道为什么……

【问题讨论】:

  • 关闭应用,看看是否收到通知?不要通过从最近滑动来关闭。正常关闭即可
  • @MohammedAtif,不,没有收到通知
  • 你检查过logcat吗?用“MFMS”过滤logcat
  • @MohammedAtif,呵呵,我的logcat里没有这样的日志……你知道吗,为什么?
  • 我从 Firebase 服务发送消息,您的意思是从 Firebase 控制台发送消息吗?

标签: java android firebase firebase-cloud-messaging firebase-notifications


【解决方案1】:

在你的 onTokenRefresh() 添加 sendRegistrationToServer(refreshedToken) 并实现它。

【讨论】:

【解决方案2】:

收到消息时在 MyFirebaseMessagingService 类中创建通知:

sendNotification("Hello","FCM");

//method

        private void sendNotification(String messageTitle, String messageBody) {
                Intent intent = new Intent(this, SplashActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

                Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(!Utils.isStringNullOrEmpty(messageTitle) ? messageTitle : APP.NAME)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))

                        .setContentIntent(pendingIntent);
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(1, notificationBuilder.build());
   }

【讨论】:

  • 我的应用没有进入 onMessageReceived 函数。你知道为什么吗?
  • 你的测试怎么样?您正在测试什么帖子消息?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
相关资源
最近更新 更多