1. 程式人生 > >極光推送獲取不到RegisterId(註冊ID)Android

極光推送獲取不到RegisterId(註冊ID)Android

官方文件介紹了兩種獲取RegistrationID的方法:
https://docs.jiguang.cn/jpush/client/Android/android_api/#registrationid-api
//SDK 初次註冊成功後,開發者通過在自定義的 Receiver 裡監聽 Action - cn.jpush.android.intent.REGISTRATION 來獲取對應的 RegistrationID。註冊成功後,也可以通過此函式獲取
public static String getRegistrationID(Context context)

在下看來第一種是誤導,應該在application中才能拿到!!!而不是自定義Receiver!在application中對其進行的初始化!!

 registrationId = JPushInterface.getRegistrationID(this);
         Log.e("1099", "run:--------->registrationId: "+registrationId );

public class MyApplication extends Application {
    public static String registrationId;

    private List<Activity> mList = new LinkedList<Activity>();
    public
static MyApplication instance; private static Context context; @Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); JPushInterface.init(this); registrationId = JPushInterface.getRegistrationID(this); Log.e("1099"
, "run:--------->registrationId: "+registrationId ); SharePLogin mSharePLogin=new SharePLogin(this); mSharePLogin.saveData("registrationId",registrationId); Toast.makeText(instance, "registrationId", Toast.LENGTH_SHORT).show(); instance = this; OkHttpClient okHttpClient = new OkHttpClient.Builder() // .addInterceptor(new LoggerInterceptor("TAG")) .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) //其他配置 .build(); OkHttpUtils.initClient(okHttpClient); MobclickAgent.setScenarioType(getApplicationContext(), MobclickAgent.EScenarioType.E_UM_NORMAL); } public MyApplication() { } public static String getRegistrationId() { return registrationId; } public static void setRegistrationId(String registrationId) { MyApplication.registrationId = registrationId; } public synchronized static MyApplication getInstance() { if (null == instance) { instance = new MyApplication(); } return instance; } public static Context getContext(){ return context; } // add Activity public void addActivity(Activity activity) { System.out.println("add"); mList.add(activity); } public void exitOther() { for (int i = 0; i < mList.size() - 1; i++) { mList.get(i).finish(); } } public void exit() { try { for (Activity activity : mList) { if (activity != null) activity.finish(); } } catch (Exception e) { e.printStackTrace(); } finally { System.exit(0); } } public void onLowMemory() { super.onLowMemory(); System.gc(); } }