私はある種のSMSアプリケーションを持っています。したがって、電話が新しいメッセージを受信するたびに、通知が表示され、クリックするとアクティビティが開始されます。現在、メッセージを 1 つ受信すると、通知され、ステータス バーで削除され、アクティビティは起動されません。ただし、2 つ以上のメッセージを受信した場合、最初の通知はクリックしても起動できませんが、残り (2 回目、3 回目の通知...) は起動できます。以下は私のコードです。
Intent newIntent = new Intent(context, PreviewActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK+0);
newIntent.setAction(strFxPrimaryKey);
Bundle newBundle = intent.getExtras();
newBundle.putInt(GlobalConstants.PRIMARY_ID, Integer.parseInt(strFxPrimaryKey));
newIntent.putExtras(newBundle);
int icon = R.drawable.notification_fx;
CharSequence tickerText = context.getString(R.string.fx_received);
long when = System.currentTimeMillis();
Notification newNotification = new Notification(icon, tickerText, when);
newNotification.flags |= Notification.FLAG_AUTO_CANCEL; //| Notification.FLAG_ONGOING_EVENT;
PendingIntent contentIntent = PendingIntent.getActivity(context, Integer.parseInt(strFxPrimaryKey), newIntent, 0);
newNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
String newNotificationService = Context.NOTIFICATION_SERVICE;
NotificationManager newNotificationManager = (NotificationManager) context.getSystemService(newNotificationService);
newNotificationManager.notify(Integer.parseInt(strFxPrimaryKey), newNotification);
context.startActivity(newIntent);
context.removeStickyBroadcast(intent);
ここでのスタックオーバーフローの回答に基づいて、一意の意図を作成するには、一意のアクションが必要であるため、主キーをアクションとして設定します。また、要求コードを主キーに設定して、一意の保留中の意図を持ちます。私のコードに欠けているものはありますか? ありがとう。
EDITED ちなみに、context.startActivity(newIntent);を削除するたびに 、正しく動作します。誰でも理由を教えてもらえますか? ありがとう。