这是我的代码:
GCMIntentService:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.putExtra("screen", activityToShow);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(smallIconToDisplayInGCM)
.setLargeIcon(largeIconToDisplayInGCM)
.setContentTitle(title)
.setContentText(text)
.setSound(sound)
.setTicker(ticker)
.setContentIntent(intent2)
.setAutoCancel(true);
主要活动:
的onCreate()
onNewIntent(getIntent())
onCreate()之外的新方法
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String tabNumber = "";
if(extras != null) {
tabNumber = extras.getString("screen");
Log.d("TEMP", "Tab Number: " + tabNumber);
if(tabNumber.equals("") || (tabNumber == null)) {
// Set the active tab to be the Home Tab (index = 0)
mTabHost.setCurrentTab(0);
} else {
mTabHost.setCurrentTab(getTabToShow(tabNumber));
}
} else {
mTabHost.setCurrentTab(0);
}
}
当我点击推送通知时,它甚至没有点击Log.d()。
我已经稍微修改了你的代码来进行一些调试:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.putExtra("screen", "debugScreen");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationManager nMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Some Title")
.setContentText("Some text")
.setContentIntent(intent2)
.setAutoCancel(true);
nMgr.notify(0, nBuilder.build());
和onNewIntent方法:
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String tabNumber;
if(extras != null) {
tabNumber = extras.getString("screen");
Log.d("TEMP", "Tab Number: " + tabNumber);
} else {
Log.d("TEMP", "Extras are NULL");
}
}
我可以检查它是否正常工作,并将变量传递给intent中的活动。
我认为您可能做错了的是使用变量activityToShow设置intent数据。考虑到Intent.putExtra(...)是一个重载方法,如果你传递一个Integer,你将无法获得一个String。
我建议您监视activityToShow变量的状态,并向接收器添加一些日志记录,以检查您获得的内容(包括变量类型)以及您在活动中收到的内容。
希望能帮助到你。