我正在开发一个Android应用程序,我需要在其中接收多个通知,每个通知都包含唯一数据。
我已经完成的事情 1.使用gcm服务接收多个通知 2.显示服务器在单击新活动中的通知时发送的数据
问题: 假设我收到了三个通知,每个通知都有唯一的数据。当我单击一个通知时,新活动将以该通知中的数据开始。所以现在接收通知活动正在运行。现在,如果我点击第二个通知接收通知活动加载旧数据(来自第一个通知的那个)。如果我关闭应用程序并单击第三个通知,则接收通知活动将加载来自第三个通知的数据,因为它已被禁止。
我试过了:
我正在使用
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
在intentservice中,在清单文件中添加android:launchMode="singleInstance"
两者都无效。
intentservice类
public class GcmIntentService extends IntentService{
Context context;
public static int notify_no=0;
//System.currentTimeMillis();
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public static final String TAG = "GCM NOTIFICATION";
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
// sendNotification(RegIdDTO.REG_ID,"Send error: " + extras.toString());
sendNotification(this,msg);
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
// sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " +
// extras.toString());
sendNotification(this,msg);
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
// sendNotification(RegIdDTO.REG_ID,msg);
sendNotification(this,msg);
Log.i(TAG, "Received: " + extras.toString());
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private static void sendNotification(Context context,String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationCompat.Builder nBuilder;
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("header")
.setLights(Color.BLUE, 500, 500).setContentText(message)
.setAutoCancel(true).setTicker("Notification from Traffic")
.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
.setSound(alarmSound)
;
String consumerid = null;
Integer position = null;
// write your click event here
Intent resultIntent = new Intent(context, NotificationReceiveActivity.class);
resultIntent.putExtra("message", message);
// resultIntent.setData(Uri.parse("content://"+when));
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Show the max number of notifications here
if (notify_no < 9) {
notify_no = notify_no + 1;
} else {
notify_no = 0;
}
nBuilder.setContentIntent(resultPendingIntent);
NotificationManager nNotifyMgr = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
nNotifyMgr.notify(notify_no + 2, nBuilder.build());
}
}
接收活动
public class NotificationReceiveActivity extends Activity {
TextView name;
TextView deal;
TextView valid;
TextView address;
JSONObject json;
GcmIntentService serv;
Context mContext;
// static boolean active = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_receive);
Intent intent = getIntent();
name = (TextView) findViewById(R.id.name);
deal = (TextView) findViewById(R.id.deal);
valid = (TextView) findViewById(R.id.valid);
address = (TextView)findViewById(R.id.address);
String message = intent.getExtras().getString("message");
try {
json = new JSONObject(message);
String stime = json.getString("name");
name.setText(stime);
String slecturename = json.getString("deal");
deal.setText(slecturename);
String sroom = json.getString("valid");
valid.setText(sroom);
String sfaculty = json.getString("address");
address.setText(sfaculty);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
serv=new GcmIntentService();
//serv.CancelNotification(getApplicationContext());
}
}
使用上面的代码我能够收到通知,问题是如果我的活动没有处于运行状态并且我收到了通知。此时,如果我从通知中打开活动,它会显示新数据但是活动是否正在运行以及是否同时通知到达。如果我打开此通知,则它正在加载旧数据的活动。即使我正在运行活动,我也希望在活动中显示新数据。
答案 0 :(得分:2)
当您获得新意图并且您的活动正在使用singleTop
或Intent正在使用FLAG_ACTIVITY_SINGLE_TOP
时,您的新意图实际上会传递到活动onNewIntent
。引用文档:
这是为在其包中将launchMode设置为“singleTop”的活动,或者在调用startActivity(Intent)时客户端使用FLAG_ACTIVITY_SINGLE_TOP标志而调用的。在任何一种情况下,当在活动堆栈的顶部而不是正在启动的活动的新实例重新启动活动时,将在现有实例上使用用于重新启动的Intent调用onNewIntent()它
在接收新意图之前,活动将始终暂停,因此您可以指望在此方法之后调用onResume()。
请注意,getIntent()仍会返回原始的Intent。您可以使用setIntent(Intent)将其更新为此新Intent。
因此,如果您在NotificationReceiveActivity
中覆盖该方法,则应解决您的问题。
答案 1 :(得分:0)
在您的活动中覆盖onNewIntent
。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
intent.getExtra("message", message);
}