簡體   English   中英

如何存儲Android設備令牌?

[英]How can I store the Android device token?

我想以某種方式存儲設備令牌,因為以后可能需要它。 例如,用戶啟動我的應用程序,執行OnNewToken,然后獲得設備令牌。 現在,我想存儲令牌,因為當用戶在幾天后重新啟動我的應用程序時,可能再次需要令牌。 我認為當用戶重新啟動我的應用程序時,不會再次執行OnNewToken,但是我不確定嗎? 因此,我想存儲令牌,並且如果再次執行OnNewToken來存儲新令牌,則僅覆蓋其值。

此外,我想在另一個類(例如Game1.cs)中使用Android設備令牌。

如何存儲設備令牌並在另一個類中獲取字符串?

我試圖用SharedPrefManager存儲令牌,但是它不起作用。 我收到錯誤消息,但不知道如何解決該問題:

SharedPrefManager.getInstance(getApplicationContext()).saveDeviceToken(token);

錯誤CS0103:名稱“ SharedPrefManager”在當前上下文中不存在

錯誤CS0103:名稱“ getApplicationContext”在當前上下文中不存在

MyFirebaseMessagingService.cs:

using System;
using Android.App;
using Android.Content;
using Android.Util;
using Firebase.Messaging;
using ggdgdgd.Android;
using System.Collections.Generic;
using Android.Support.V4.App;

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

    public override void OnNewToken(string token)
    {
        Log.Debug(TAG, "Refreshed token: " + token);
        storeToken(refreshedToken);
    }

    private void storeToken(String token)
    { 
        SharedPrefManager.getInstance(getApplicationContext()).saveDeviceToken(token);
    }

    public override void OnMessageReceived(RemoteMessage message)
    {
        Log.Debug(TAG, "From: " + message.From);
        var body = message.GetNotification().Body;
        var title = message.GetNotification().Title;
        Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
        SendNotification(body, title, message.Data);
    }

    void SendNotification(string messageBody, string Title, IDictionary<string, string> data)
    {
        var intent = new Intent(this, typeof(Activity1));
        intent.AddFlags(ActivityFlags.ClearTop);
        foreach (var key in data.Keys)
        {
            intent.PutExtra(key, data[key]);
        }

        var pendingIntent = PendingIntent.GetActivity(this,
                                                      Activity1.NOTIFICATION_ID,
                                                      intent,
                                                      PendingIntentFlags.OneShot);

        var notificationBuilder = new NotificationCompat.Builder(this, Activity1.CHANNEL_ID)
                                  .SetSmallIcon(Resource.Drawable.Icon)
                                  .SetContentTitle(Title)
                                  .SetContentText(messageBody)
                                  .SetAutoCancel(true)
                                  .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManagerCompat.From(this);
        notificationManager.Notify(Activity1.NOTIFICATION_ID, notificationBuilder.Build());
    }
}
}

SharedPreferences等效的Xamarin.Android是一個稱為ISharedPreferences的接口。

您可能會這樣:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Android.App.Application.Context)            
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutString ("key_for_your_token", token);
// editor.Commit();    // older APIs
editor.Apply();   // newer APIs

並獲得值:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context)            
string token = prefs.GetString ("key_for_your_token", "");

暫無
暫無

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

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