我创建一个notification
但使用发送时,它PendingIntent
的extras
得到在这个过程中丢失。 我使用的是singleTop Activity
,所以我在设置一个断点onNewIntent
也是我handleIntent
,看看哪一个是射击,当我点击了通知。
当我点击该通知,它解雇了我handleIntent
方法。 我想到了一个singleTop
活动将火onNewIntent
方法?
为什么我的Intent extras
创建时丢失在这个过程中notification
? 我调试的intent
,它显示了Bundle[mParcelledData.dataSize=168]
如何unparcel
的包?
处理该意向活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mPrefs = new SharedPrefs(this);
mDb = DatabaseManager.getInstance(this);
mVolleyManager = new VolleyManager(this);
fm = getSupportFragmentManager();
setupToolbar();
setupNavDrawer();
setupBroadcastReceivers();
setupUniversalImageLoader();
if (savedInstanceState == null) {
FragmentTransaction ft = fm.beginTransaction();
ft = ViewManager.setFragmentAnimations(ft);
if (!mPrefs.getDemoMode()) {
if (!mPrefs.getLoggedIn()) {
ft.add(R.id.container, LoginFragment.newInstance(), GlobalVars
.TAG_LOGIN_FRAGMENT).commit();
}
else {
ft.add(R.id.container, BrowseFragment.newInstance(), GlobalVars.
TAG_BROWSE_FRAGMENT).commit();
}
}
}
// Handles intents mainly sent from GCM notification click events
handleIntent();
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent();
}
private void handleIntent() {
Intent intent = getIntent();
if (intent.hasExtra(GlobalVars.KEY_EVENT)) {
handleGcmIntent(intent);
}
}
添加额外的意图和创建通知
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(GlobalVars.KEY_EVENT, events);
intent.putExtra(GlobalVars.KEY_GAME_DATE_ID, gameDate.getId());
String text = getResources().getString(R.string.msg_found_match);
createNotification(text, intent);
private void createNotification(String text, Intent intent) {
mNotificationManager = (NotificationManager) this.getSystemService(Context
.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("LovelUp")
.setContentText(text)
.setAutoCancel(true)
.setLights(R.color.primary_blue, 2000, 2000);
if (mPrefs.getNotificationVibrate()) {
mBuilder.setVibrate(new long[] {1000, 1000, 1000});
}
if (mPrefs.getNotificationSound()) {
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
}
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent
.FLAG_CANCEL_CURRENT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}