簡體   English   中英

xamarin android 推送通知只能工作一次

[英]xamarin android push notification only works once

我正在開發一個 android 應用程序,我按照這個鏈接使用谷歌推送通知
我的問題是該應用程序第一次啟動時,推送通知工作正常,但是當我重新啟動我的應用程序時,推送通知停止工作並且我無法接收通知。

這是我的代碼:

[Service(Exported = false)]
class RegistrationIntentService : IntentService
{
    static object locker = new object();

    public RegistrationIntentService() : base("RegistrationIntentService") { }


    protected override void OnHandleIntent(Intent intent)
    {
        try
        {
            Log.Info("RegistrationIntentService", "Calling InstanceID.GetToken");
            lock (locker)
            {
                var instanceID = InstanceID.GetInstance(this);
                var token = instanceID.GetToken(
                    "my_id", GoogleCloudMessaging.InstanceIdScope, null);

                Log.Info("RegistrationIntentService", "GCM Registration Token: " + token);
                SendRegistrationToAppServer(token);
                Subscribe(token);
            }
        }
        catch (Exception e)
        {
            Log.Debug("RegistrationIntentService", "Failed to get a registration token");
            return;
        }
    }


    void SendRegistrationToAppServer(string token)
    {
     // some code
    }

    void Subscribe(string token)
    {
        var pubSub = GcmPubSub.GetInstance(this);
        pubSub.Subscribe(token, "/topics/global", null);
    }
}


[Service(Exported = false), IntentFilter(new[] { "com.google.android.c2dm.intent.RECEIVE" })]
public class MyGcmListenerService : GcmListenerService
{
    public override void OnMessageReceived(string from, Bundle data)
    {
        Log.Debug("MyGcmListenerService", "From:    " + from);
        Log.Debug("MyGcmListenerService", "Message: " + message);
    }
}


[Service(Exported = false), IntentFilter(new[] { "com.google.android.gms.iid.InstanceID" })]
class MyInstanceIDListenerService : InstanceIDListenerService
{
    public override void OnTokenRefresh()
    {
        var intent = new Intent(this, typeof(RegistrationIntentService));
        StartService(intent);
    }
}

我用這種方法發送通知

public static void SendNotification(string MESSAGE, string token)
    {
        var jGcmData = new JObject();
        var jData = new JObject();
        jData.Add("message", MESSAGE);

        jGcmData.Add("to", token);
        jGcmData.Add("data", jData);

        var url = new Uri("https://gcm-http.googleapis.com/gcm/send");
        try
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.TryAddWithoutValidation(
                    "Authorization", "key=" + API_KEY);

                System.Threading.Tasks.Task.WaitAll(client.PostAsync(url,
                    new StringContent(jGcmData.ToString(), Encoding.Default, "application/json"))
                        .ContinueWith(response =>
                        {
                            Console.WriteLine(response);
                            Console.WriteLine("Message sent: check the client device notification tray.");
                        }));
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Unable to send GCM message:");
            Console.Error.WriteLine(e.StackTrace);
        }

    }


你有什么主意嗎?

這是在模擬器中嗎? 我在模擬器上遇到了這個問題,並通過關閉在部署之間保留應用程序數據/緩存的選項來修復它。 在 Visualstudio 中,它是工具->選項-Xamarin->Android 選項。 現在我在每次部署時都會得到一個新令牌,並且它始終有效。

為什么會發生這種情況我還沒有弄清楚。 也許這是一個安全功能,不同版本的應用程序不能使用相同的版本?

來自 Xamarin 的官方文檔:

“如果您強制關閉應用程序,FCM 將停止發送通知。Android 防止后台服務廣播無意或不必要地啟動已停止應用程序的組件。(有關此行為的更多信息,請參閱已停止應用程序的啟動控件。)因此,每次運行應用程序時都必須手動卸載應用程序並從調試會話中停止它——這會迫使 FCM 生成一個新令牌,以便繼續接收消息。”

https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM