我有一个广播接收器,用于监听云消息。我可以正常显示通知,但当用户点击通知时,我希望使用intent extras将他们路由到正确的活动页面。但extras始终为空。
在调试过程中,我可以验证传递给pending intent的intent确实具有extras。
我尝试在活动的onCreate()方法中捕获意图。
***编辑添加可运行代码,有几点需要注意,如果任务已经启动,则不能保证oncreate()方法被调用,因此最好在onresume方法中获取extras。我需要设置意图和pending intent上的标志。
在调试过程中,我可以验证传递给pending intent的intent确实具有extras。
我尝试在活动的onCreate()方法中捕获意图。
***编辑添加可运行代码,有几点需要注意,如果任务已经启动,则不能保证oncreate()方法被调用,因此最好在onresume方法中获取extras。我需要设置意图和pending intent上的标志。
@EReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "googlecloudmessage";
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
private Context ctx;
@Pref
public MyPrefs_ myPrefs;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received gcm message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
this.ctx = context;
String messageType = gcm.getMessageType(intent);
if (messageType != null) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
}
else if (messageType.equalsIgnoreCase("gcm")) {
myPrefs.notification().put(true);
handleNotification(intent);
}
}
setResultCode(Activity.RESULT_OK);
}
private void handleNotification(Intent intent) {
Bundle bundle = intent.getExtras();
String typeString = bundle.getString("type");
String icon = bundle.getString("icon");
String title = null;
String body = null;
Class<?> intentClass = null;
Integer intentExtraId = null;
if (typeString == null)
return;
int type = Integer.parseInt(typeString);
switch (type) {
case 1:
intentClass = FriendsActivity_.class;
title = bundle.getString("username");
break;
case 2:
intentClass = UserProfileActivity_.class;
title = bundle.getString("username");
intentExtraId = Integer.parseInt(bundle.getString("user_id"));
break;
}
mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent i = new Intent();
i.setClass(ctx, intentClass);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("gcm", true);
if (intentExtraId != null) {
i.putExtra("id", intentExtraId);
}
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, requestID, i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);
mBuilder.setSmallIcon(R.drawable.ic_stat_u);
Bitmap bmp = null;
try {
bmp = Ion.with(ctx, ctx.getResources().getString(R.string.image_container) + icon).asBitmap().get();
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (ExecutionException e) {
e.printStackTrace();
}
if (icon != null) {
mBuilder.setLargeIcon(bmp);
}
mBuilder.setContentTitle(title).setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
活动
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
Integer newId = getIntent().getExtras().getInt("id");
id = newId;
}
onCreate()
中获取额外信息所使用的代码。 - David Wasser