2
我希望接收推送通知(c2dm)接收顯示通知。此通知開始,PendingIntent,一個顯示彈出窗口的活動。點擊「確定」按鈕後,此彈出窗口啓動我的應用程序。通知PendingIntent不會再次呼叫我的活動
這裏是在推送通知前臺執行的代碼:
private void dealMessage(Context context, Intent intent)
{
try
{
String message = intent.getExtras().getString("message");
Log.v("testc2dm","message : [" + message + "]");
//create bean of my Notification
NotificationPush notif = new NotificationPush();
notif.init((JSONObject)JSONValue.parse(message));
//create PendingIntent
Intent intentDialog = new Intent(context, DialogActivity.class);
intentDialog.putExtra("notif", notif);
int requestCode= (int) System.currentTimeMillis();
PendingIntent pi = PendingIntent.getActivity(context, requestCode, intentDialog, PendingIntent.FLAG_ONE_SHOT);
//Create the Notification
Notification n = new Notification();
n.flags |= Notification.FLAG_SHOW_LIGHTS; // allume l'écran
n.flags |= Notification.FLAG_AUTO_CANCEL; // fait disparaitre automatiquemet la notif apres un clic dessus
n.defaults = Notification.DEFAULT_ALL;
n.icon = R.drawable.icon;
n.when = System.currentTimeMillis();
n.setLatestEventInfo(context, "Mon titre", notif.getTitre(), pi);
//add my Notification
NotificationManager notificationManager = (NotificationManager)context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_NOTIFICATION, n);
}
catch(Exception e)
{
Log.e("testc2dm","error : [" + e.getMessage() + "]");
e.printStackTrace();
}
}
這是我的活動,顯示彈出式窗口,啓動我的應用程序:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.v("testc2dm","DialogActivity oncreate()");
//recovery of my bean Notification
Intent intentParam = getIntent();
NotificationPush notif = (NotificationPush)intentParam.getSerializableExtra("notif");
if(notif != null)
{
Log.v("testc2dm","notif => titre [" + notif.getTitre() + "] -- msg [" + notif.getMessage() + "] -- type [" + notif.getType() + "]");
//display popup
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(notif.getMessage());
builder.setTitle(notif.getTitre());
builder.setCancelable(false);
builder.setNegativeButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
if(!TestC2DMActivity.appIsRunning)
{
//launch first Activity of my application
Intent intent = new Intent(DialogActivity.this, TestC2DMActivity.class);
startActivity(intent);
}
}
});
builder.show();
}
}
我的問題是:如果我的應用將通過接收推送(c2dm> Notification> PendingIntent> DialogActivity> TestC2DMActivity)啓動,然後接收下一個推送,Notification將顯示正常,但單擊Notification將不會啓動DialogActivity。而當應用程序正常啓動(應用程序的圖標),然後一切正常。
我覺得如果我的應用程序是由PendingIntent啓動的,那麼這個PendingIntent不再需要啓動DialogActivity。爲什麼?
真的感謝您的幫助,我的英語不好對不起..