android firebase通知不起作用

问题描述 投票:1回答:2

我使用我的应用设置了Firebase消息,但遗憾的是通知未到来。

我正确设置了Firebase,它连接到我的应用程序,我还发送了一些测试消息,在Firebase中说它已完成,但我没有在手机上收到它们。

我的应用程序尚未在商店中,我正在通过Android工作室开发和测试它。

这是我的MyFirebaseInstanceIDService类

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */
// [START refresh_token]
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);


    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}

}

这里是MyFirebaseMessagingService类:

公共类MyFirebaseMessagingService扩展FirebaseMessagingService {private static final String TAG =“MyFirebaseMsgService”;

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]


    //"Title","Message","NotyType",   "hotelStatus"

    String title = "";
    if (remoteMessage.getNotification().getTitle() != null){
        title = remoteMessage.getNotification().getTitle();
    }

    String message = "";
    if (remoteMessage.getNotification().getBody() != null){
        message = remoteMessage.getNotification().getBody();
    }

    Log.e("notification","recieved");


    sendNotification(title, message);
    // 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.
}
// [END receive_message]


private void sendNotification(String title, String body) {
    Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pi = PendingIntent.getActivity(this,
            0 /* Request code */,
            i,
            PendingIntent.FLAG_ONE_SHOT);

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
            getString(R.string.default_notification_channel_id))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(sound)
            .setContentIntent(pi);

    NotificationManager manager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    assert manager != null;
    manager.notify(0, builder.build());
} }

调试时我没有看到任何日志,也没有在手机上收到任何通知。

我在这里做错了吗?你能给些建议么?谢谢。

android firebase notifications
2个回答
1
投票

使用仅通知标记发送的推送不会调用onMessageReceived,并且您的应用程序不在前台。如果确实在前台,则会调用onMessageReceived。

如果您希望触发onMessageReceived,则需要使用附加数据标记或数据标记发送推送。

但请注意,如果同时发送通知和数据标记,则只有当您的应用位于前台时才会触发onMessageReceived,如果它位于后台,则数据标记内的所有内容都将作为附加内容传递到点击意图内

无论您的app是否处于前台,只有Data标记始终会调用onMessageReceived。

例如:仅用于数据标签:)

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

1
投票

哦,现在我尝试了一下!正如您在我的问题中看到的那样,我更新了MyFirebaseMessagingService类,现在它可以工作了!从Firebase控制台,我只发送了消息,没有数据,以及我在设备中收到的所有通知!即使我的应用程序正在运行,即使我关闭它!我收到的每个通知。当应用程序运行时,通知具有我的自定义图标,当应用程序关闭时,默认的“铃声”图标就在那里,但在这两种情况下我都收到了通知!不确定它是如何可能的,但它确实有效。

© www.soinside.com 2019 - 2024. All rights reserved.