我正在开发Android GCM推送通知。下面是我的代码,在这段代码中我试图在“redirectUrl”值的besis上的两个不同的页面上我的页面(我从gcm得到这个)
但是当我每次尝试在我的Activity上获得intent.getExtras()时(第一个通知绑定值)。假设我有第一次通知导航到主页它工作正常,但下次如果我收到了个人资料页面的通知,但它从getIntent.getExtra()包中获得了回家。
GCMBroadcastReceiver.java
public class GCMBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(action.equalsIgnoreCase("com.google.android.c2dm.intent.REGISTRATION")){
//code when device will registered with GCM
}else if(action.equalsIgnoreCase("com.google.android.c2dm.intent.RECEIVE")){
Bundle bundle=intent.getExtras();
String notificationMessage=intent.getExtras().getString("notificationMessage","");
NotificationManager NM=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.ic_launcher,"Message",System.currentTimeMillis());
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alarmSound!=null)
notify.sound = alarmSound;
String customerID=bundle.getString("customerID","0");
String siteID=bundle.getString("siteID","0");
PendingIntent pending=PendingIntent.getActivity(context,0, getIntent(context,bundle.getString("redirectUrl","")),0);
notify.setLatestEventInfo(context,"Message",notificationMessage,pending);
notify.flags=Notification.FLAG_AUTO_CANCEL;
int i= new Random().nextInt(1000 - 0 + 1);
NM.notify(i, notify);
}
}
public Intent getIntent(Context context,String redirectUrl){
Intent intent=null;
intent=new Intent(context, AltaDataActivity.class);
if(redirectUrl.equalsIgnoreCase("Home")){
intent.putExtra("selectedSliderID", "Home");
}else{
intent.putExtra("selectedSliderID", "Profile");
}
return intent;
}
}
活动
public class AltaDataActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alta_data);
selectedSliderID="Home";
Bundle notificationBundle=getIntent().getExtras();
if(bindedBundleWithIntent!=null){
selectedSliderID=notificationBundle.getString("selectedSliderID");
System.out.println(selectedSliderID);
}
//open page on the besis of selectedSliderID value
}
}
答案 0 :(得分:1)
接收器类的问题
PendingIntent pending=PendingIntent.getActivity(context,0, getIntent(context,bundle.getString("redirectUrl","")),0);
在这一行中,第二个参数是requestCode,因此我们不应该使用相同的请求创建待处理的意图,因此为所有Pending意图提供不同的意图。和最后一个参数而不是0使用
PendingIntent.FLAG_UPDATE_CURRENT