2012-02-09 10 views
19

C2DMから送信され、アプリで受信された通知を作成すると、C2DMのプッシュ通知に付属のデータの一部がインテントエクストラ。これは初めて通知を開くときにうまくいきます。次に、データは、アクティビティの状態に応じてonNewIntentまたはonCreateで受信されます。Androidステータスバーの通知 - 2回目の古いエクストラを取得する意図

しかし、C2DMで2回目のプッシュ通知を送信すると、新しいデータが正しく受信されますが、意図からエキストラを取得すると、以前のメッセージからデータが取得されます。見たいタイトルとデータが通知に正しく表示されます。だから私の意図は間違っているはずです。これは、アクティビティーが実行中で、アクティビティーが実行中でない場合に発生します。

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.ic_stat_notify_push, "Message received", System.currentTimeMillis()); 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
notification.defaults |= Notification.DEFAULT_LIGHTS; 
Intent intent = new Intent(context, DesktopApp.class); 
intent.setAction("android.intent.action.MAIN"); 
intent.addCategory("android.intent.category.LAUNCHER"); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
intent.putExtra("msg_id", msg_id); 
intent.putExtra("title", title); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
notification.setLatestEventInfo(context, "New message", title + String.valueOf(msg_id), pendingIntent); 
notificationManager.notify(0, notification); 

その後、意図読むには:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Intent myIntent = getIntent(); // this is just for example purpose 
    int i = myIntent.getIntExtra("msg_id", -1); 
    if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show(); 
} 


@Override 
public void onNewIntent(Intent intent){ 
    super.onNewIntent(intent); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     int i = extras.getInt("msg_id", -1); 
     if (i != -1) Toast.makeText(this, "Got message! " + String.valueOf(i), Toast.LENGTH_LONG).show(); 
    } 
} 

任意の提案を、私は次の操作を行い、通知を作成するには

+0

感謝onNewIntent()メソッドは、私の問題を解決:) – SAndroidD

答えて

37

あなたはPendingIntentにクリーンフラグを設定する必要があります。

PendingIntent pintent = 
    PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT); 

長い説明を与える表情at this postを持っています。

5

使用この

PendingIntent pi = PendingIntent.getActivity(context, UNIQUE_ID, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT); 
関連する問題