Firebase(Xamarin - Android) - 为什么只有OnMessageReceived才会在我删除FirebaseInstanceID后再次出现?

时间:2017-01-26 01:15:09

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

为什么只有在删除FirebaseinstanceID后再次发生OnMessageReceived?

我想做的事情非常简单。我想这样做,所以我不断使用FirebaseMessagingService获取消息。

我目前的情况是:

  • 我是第一次启动应用程序(或者在设备上卸载或删除FirebaseInstance ID后)
  • 它会生成一个新的InstanceID和一个令牌。
  • 我可以向应用程序发送消息 - 它接收它们就好了。

如果我停止应用并再次运行(不执行删除FirebaseInstanceID之类的操作) - 我将获得相同的FirebaseInstanceID(这是有道理的)但我的OnMessageReceived将不再接收消息。

在阅读完我在网络上可能找到的所有内容之后,以及此处 - 我无法回答这个具体问题 - 为什么在删除FirebaseinstanceID后仅再次发生OnMessageReceived? < / p>

public override void OnMessageReceived(RemoteMessage message) 

仅在我的主Activity

中删除Firebase InstanceID时才会触发
var instanceID = FirebaseInstanceId.Instance;
instanceID.DeleteInstanceId();

我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="firebasetesttwo.firebasetesttwo" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application android:label="firebasetesttwo" android:icon="@drawable/Icon">

    <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>

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading.Tasks;
using Firebase.Iid;
using System;

namespace firebasetesttwo
{
    [Activity(Label = "firebasetesttwo", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            Task.Run(() =>
            {

                var instanceID = FirebaseInstanceId.Instance;


            });

            // Set our view from the "main" layout resource
            // SetContentView (Resource.Layout.Main);
        }
    }
}

我的FirebaseIDService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Firebase.Iid;

namespace firebasetesttwo
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    class MyFirebaseIdService:FirebaseInstanceIdService
    {

        public override void OnCreate()
        {
            base.OnCreate();
        }

        public override void OnTaskRemoved(Intent rootIntent)
        {
            base.OnTaskRemoved(rootIntent);
        }


        public override bool OnUnbind(Intent intent)
        {
            return base.OnUnbind(intent);
        }



        public override void OnTokenRefresh()
        {
            base.OnTokenRefresh();

            // We can log things buyt we are not going to worry about it at this point 
        }
    }
}

**MyFirebaseMessagingService.cs**

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Firebase.Messaging;
using Android.Media;
using Android.Support.V4;
using Android.Support.V4.App;
using Android;
using firebasetesttwo;

namespace firebasetesttwo
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT"})]
    class MyFirebaseMessagingService : FirebaseMessagingService
    {
        public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);

        }

    }
}

0 个答案:

没有答案