我这样实施:
public static void setPushSetting(Context context, boolean flag) {
if(flag) {
FirebaseInstanceId.getInstance().getToken();
} else {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
} catch (IOException e) {
RxBus.getInstance().post(new OffPushError());
e.printStackTrace();
}
}
}
昨天,它有效,但今天FirebaseInstanceId.getInstance().deleteInstanceId();
总是抛出异常。
是否还有其他方法可以在禁用和实施方面实施启用Firebase推送通知?谢谢你的帮助!
-------更新异常的日志-------------
java.io.IOException: MAIN_THREAD
at com.google.firebase.iid.zzd.zzb(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceId.deleteInstanceId(Unknown Source)
at jp.seesaa.itmama.push.PushUtil.setPushSetting(PushUtil.java:32)
at jp.seesaa.itmama.ui.main.setting.SettingFragment.onSharedPreferenceChanged(SettingFragment.java:66)
at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:476)
at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:384)
at android.support.v4.content.EditorCompatGingerbread.apply(EditorCompatGingerbread.java:25)
at android.support.v4.content.SharedPreferencesCompat$EditorCompat$EditorHelperApi9Impl.apply(SharedPreferencesCompat.java:45)
at android.support.v4.content.SharedPreferencesCompat$EditorCompat.apply(SharedPreferencesCompat.java:67)
at android.support.v7.preference.Preference.tryCommit(Preference.java:1353)
at android.support.v7.preference.Preference.persistBoolean(Preference.java:1558)
at android.support.v7.preference.TwoStatePreference.setChecked(TwoStatePreference.java:79)
at android.support.v7.preference.TwoStatePreference.onClick(TwoStatePreference.java:64)
at android.support.v7.preference.Preference.performClick(Preference.java:971)
at android.support.v7.preference.Preference.performClick(Preference.java:957)
at android.support.v7.preference.SwitchPreferenceCompat.performClick(SwitchPreferenceCompat.java:203)
at android.support.v7.preference.Preference$1.onClick(Preference.java:145)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:6)
我通过以下其他线程运行来修复它:
Thread thread = new Thread(() -> {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
} catch (Exception e) {
Log.e("ERROR", Log.getStackTraceString(e) );
RxBus.getInstance().post(new OffPushError());
}
});
thread.start();
答案 1 :(得分:5)
删除Firebase实例ID和令牌
删除firebase instance-id必须在后台线程中调用
AsyncTask.execute(() -> {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
Log.d("TAG","Fb deleteInstanceId!");
} catch (IOException e) {
Log.e("TAG","Error: Fb deleteInstanceId:" + e.toString());
}
});
答案 2 :(得分:2)
我也有同样的问题,看起来像deleteInstanceId();
需要执行异步。我使用快速的AsynTask类解决了它。
private class DeleteTokenTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... voids) {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
} catch (IOException e) {
Log.d(TAG, "Exception deleting token", e);
}
return null;
}
}
然后我从任何地方都这样称呼它:new DeleteTokenTask().execute();
它运行良好。