【问题标题】:Firebase Messaging ServiceFirebase 消息服务
【发布时间】:2017-06-16 16:14:54
【问题描述】:

我正在实施基于火的消息传递服务向所有用户发送通知。到目前为止一切都很好,但问题是当我关闭我的 WiFi 并同时从 fire-base 控制台发送通知时,我的消息状态显示消息已成功发送但当我打开我的 WiFi 时我没有收到任何通知,为什么会这样,FCM 是否不允许挂起的通知,或者我做错了什么。他们是如何发送处于待处理状态的通知的任何实现。下面是我的代码

Android 清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xamarin.fcmexample" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <application android:label="FCMClient">
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
        <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

FirebaseMessagingService.cs

    public class MyFirebaseMessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";
    public override void OnCreate()
    {
        base.OnCreate();
    }
    public override void OnMessageReceived(RemoteMessage message)
    {
        Log.Debug(TAG, "From: " + message.From);
        Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
        SendNotification(message.GetNotification().Body);
    }
    void SendNotification(string messageBody)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle("FCM Message")
            .SetContentText(messageBody)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

FirebaseIDService.cs

   public class MyFirebaseIIDService : FirebaseInstanceIdService
    {
        const string TAG = "MyFirebaseIIDService";
        public override void OnTokenRefresh()
        {
            var refreshedToken = FirebaseInstanceId.Instance.Token;
            Log.Debug(TAG, "Refreshed token: " + refreshedToken);
            SendRegistrationToServer(refreshedToken);
        }
        void SendRegistrationToServer(string token)
        {
            // Add custom implementation, as needed.
        }
    }

【问题讨论】:

  • 我使用相同的代码并尝试重现您的问题,第一次部署在我的手机上,一切正常。当我关闭 WiFi 时,虽然有短暂的延迟,但应用程序仍然收到消息。但是当我清理和重建项目时,我收不到任何消息。

标签: android firebase xamarin firebase-cloud-messaging


【解决方案1】:

我认为您的问题取决于应用程序是在前台还是在后台,以及您是发送 Notification 有效负载还是 Data 有效负载,因为当应用在后台,通知负载将不会到达“OnMessageReceived”回调。查看以下链接中的应用状态表:https://firebase.google.com/docs/cloud-messaging/android/receive

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多