我有一个应用程序,它使用FCM发送和接收消息。我已经按照文档中的建议实施了所有操作。
现在,在生成令牌时,我想将其保存到共享首选项中。但这给了错误。
考虑类的名称就像
public class MyFirebaseMsgAndTokenService extends FirebaseMessagingService {
private static final String TAG = "C_MyFirebaseIIDService";
@Override
public void onNewToken(String newToken) {
super.onNewToken(newToken);
Log.d(TAG, "Refreshed token: " + newToken);
CustomSharedPreferences customSharedPreferences = new CustomSharedPreferences(this); // giving error on this
String oldToken = customSharedPreferences.getStringData(FCM_GENERATED_TOKEN);
sendRegistrationToServer(newToken,oldToken);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
try {
} catch (Exception e) {
}
}}
我真的很想在此服务中将令牌保存到我的应用程序中。请告诉我为什么我无法获得参考/上下文?
更新:我的CustomSharedPrefernces是一个将数据保存到sharedPrefernces中的类
public class CustomSharedPreferences {
public static final String FCM_GENERATED_TOKEN = "FCM_GENERATED_TOKEN";
private static CustomSharedPreferences mCustomSharedPreferences;
private final SharedPreferences sharedPreferences;
public static CustomSharedPreferences getInstance(Context context) {
if (mCustomSharedPreferences == null) {
mCustomSharedPreferences = new CustomSharedPreferences(context);
}
return mCustomSharedPreferences;
}
public CustomSharedPreferences(Context context) {
sharedPreferences = context.getSharedPreferences("myApp", Context.MODE_PRIVATE);
}}
更新:
错误:“ CustomSharedPreferences中的android.content.context无法应用于com.myprojectname.MyFireBaseMsgAndTokenService”
答案 0 :(得分:3)
您可以在此处查看我的Firebase通知处理:
public UserPreferences(Context context) {
this.context = context;
preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
您可以简单地使用this
或getApplicationContext()
来获取上下文
答案 1 :(得分:2)
对我来说,我刚刚将Firebase Messaging升级到了最新版本(在撰写本文的这段时间是20.2.0)。
implementation 'com.google.firebase:firebase-messaging:20.2.0'
然后我突然又可以通过this
。
答案 2 :(得分:0)
添加以下代码,这是您的CustomSharedPreferences类。
@Override
public void onNewToken(String s) {
super.onNewToken(s);
@SuppressLint("HardwareIds")
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
Log.d("DEVICE_ID: " , deviceId);
Log.d("FCM_TOKEN",s);
SharedHelper.putKey(this, "device_token", s);
SharedHelper.putKey(this, "device_id", deviceId);
}
之后,在FirebaseMessagingService.class中添加以下代码
$.fn.WBslider = function() {
return this.each(function() {
var $_this = $(this),
$_date = $('input', $_this),
$_title = $('.setyear', $_this),
thumbwidth = 85, // set this to the pixel width of the thumb
yrnow = 200;
$_date.on('input change keyup', function() {
var $_this = $(this),
val = parseInt($_date.val(), 10);
if (val < 70) {
val = '< 70';
}
if (val === '') { // Stop IE8 displaying NaN
val = 0;
}
$_title.text(val);
var pos = (val - $_date.attr('min')) / ($_date.attr('max') - $_date.attr('min'));
// position the title with the thumb
var thumbCorrect = thumbwidth * (pos - 0.5) * -1,
titlepos = Math.round((pos * $_date.width()) - thumbwidth / 4 + thumbCorrect);
if ($("#date1").attr("style") != undefined) {
titlepos = 23 + parseFloat($("#date1").attr("style").split(" ").pop().split("%")[0]);
} else {
titlepos = 70;
}
$_title.css({
'left': titlepos
});
// show "progress" on the track
pos = Math.round(pos * 99); // to hide stuff behide the thumb
var grad = 'linear-gradient(90deg, #fb824f ' + pos + '%,#e2e5ec ' + (pos + 1) + '%)';
$_date.css({
'background': grad
});
}).on('focus', function() {
if (isNaN($(this).val())) {
$(this).val(0);
}
}).trigger('change');
$(window).on('resize', function() {
$_date.trigger('change');
});
});
};
$(function() {
$('.slider').WBslider();
});
不要在FirebaseMessagingService类中初始化您的CustomSharedPreferences,这可能会对您有所帮助。
答案 3 :(得分:0)
FirebaseMessagingService extends Service
和
Service extends ContextWrapper
和
ContextWrapper extends Context
所以它也是Context
,所以请使用MyFirebaseMessagingService.this
或简单地使用this
。