【发布时间】:2015-09-24 09:19:28
【问题描述】:
我有以下广播接收器:
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive");
final String token1 = intent.getStringExtra("token");
System.out.println("onReceive" + token1);
Bundle extras = getIntent().getExtras();
if (extras != null) {
final String token = intent.getStringExtra("token");
System.out.println("Token fetched");
new PostLoginWithGCM(cleanKey, token).execute();
} else {
System.out.println("Token null");
}
}
};
输出是:
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: onReceive
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: onReceivefy6gmbUODWk:APA91bHf3PrCQmzZXnfR5LU40C7Mu1jhHenX8SvYR2OvQR6A3npXKa4NF8J-eZtHoxO7QAbTe_S94L8IttuU7ZrT5S97mucJb6EmmF92y3Hi60lrGb6cBfjDHj9fYDfaL8chion-Uyh5
09-24 14:44:04.741 9103-9103/com.exa.exaCRM I/System.out: Token null
所以第一个 System.out onReceive 工作正常。
第二个 System.out 给了我与 Intent 一起传递的令牌值。
最后我检查了我的附加值是否为空,令我惊讶的是,我的附加值是空的,而我只是在空值检查之前打印了数据。
这是负责为我生成令牌和本地广播的方法:
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
InstanceID instanceID = InstanceID.getInstance(this);
token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
subscribeTopics(token);
sharedPreferences.edit().putBoolean(SessionManager.SENT_TOKEN_TO_SERVER, true).apply();
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
sharedPreferences.edit().putBoolean(SessionManager.SENT_TOKEN_TO_SERVER, false).apply();
}
Intent registrationComplete = new Intent(SessionManager.REGISTRATION_COMPLETE);
System.out.println("token before assignment" + token);
registrationComplete.putExtra("token", token.toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
【问题讨论】: