未在Android

时间:2017-07-10 10:13:48

标签: android firebase xamarin xamarin.forms firebase-cloud-messaging

我正在尝试使用FCM发送通知。我正在使用Xamarin构建应用程序,因此我按照this教程设置了我的通知。

这是我的firebase服务:

[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";
    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        System.Diagnostics.Debug.WriteLine(TAG, "Refreshed token: " + refreshedToken);
        SendRegistrationToServer(refreshedToken);
    }
    void SendRegistrationToServer(string token)
    {
        // Add custom implementation, as needed.
    }
}

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";

    public override void OnMessageReceived(RemoteMessage message)
    {
        Android.Util.Log.Debug(TAG, "From: " + message.From);
        Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
    }

    void SendNotification(string messageBody)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0 /* Request code */, intent, PendingIntentFlags.OneShot);

        var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetContentTitle("FCM Message")
            .SetContentText(messageBody)
            .SetAutoCancel(true)
            .SetSound(defaultSoundUri)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);

        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

我遇到了刷新令牌不时出现故障的问题,所以我将其添加到我的MainActivity中:

if (IsPlayServicesAvailable())
        {

            var json = "";
            using (StreamReader sr = new StreamReader(Assets.Open("google_services.json")))
            {
                json = sr.ReadToEnd();
            }
            var options_from_json = JObject.Parse(json);

            try
            {
                var options = new FirebaseOptions.Builder()
                    .SetApplicationId(options_from_json["client"][0]["client_info"]["mobilesdk_app_id"].ToString())
                    .SetApiKey(options_from_json["client"][0]["api_key"][0]["current_key"].ToString())
                    //.SetDatabaseUrl("Firebase-Database-Url")
                    .SetGcmSenderId(options_from_json["project_info"]["project_number"].ToString())
                    .Build();
                var firebaseApp = FirebaseApp.InitializeApp(this, options);
            }
            catch (IllegalStateException e)
            {
                System.Diagnostics.Debug.WriteLine("L'app firebase existe déjà, donc il n'y a rien à faire.");
            }

            await Task.Run(() =>
             {
                 var instanceID = FirebaseInstanceId.Instance;
                 var iid1 = instanceID.Token;
                 var iid2 = instanceID.GetToken(options_from_json["project_info"]["project_number"].ToString(), Firebase.Messaging.FirebaseMessaging.InstanceIdScope);
                 System.Diagnostics.Debug.WriteLine("Iid1: {0}, iid2: {1}", iid1, iid2);
             });
        }

很好,到目前为止,我已经获得了仿真器以及现实设备的令牌。 我的问题是通知的接收:它真的很危险,有时我在FCM控制台上发送后立即得到一些,有时我必须重新启动设备,有时它只需要几个小时才能到达,有时候我什么也得不到......我的设置没有改变,设备不在wifi上。

任何线索?

0 个答案:

没有答案
相关问题