单击该活动中的通知时如何启动活动

时间:2015-10-01 05:37:43

标签: android notifications

我正在开发一个游戏。我正在我的游戏中使用通知服务。我创建了这个通知,当我在另一个Activity时它完全正常。当我关闭游戏并点击收到的通知时,它会重定向到正确的活动(GameScreen.class)。但是当我在GameScreen活动中并且如果我收到通知时,GameScreen不会更新收到的值。它仍然是相同的。我尝试了很多解决方案。我的通知如下

private void newBamRecieve(String msg,String id) {
    Log.d(TAG, "Preparing send notification...: " + msg);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);


    Intent intent = new Intent(MyActivityIntentService.this, GameScreen.class);

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

              intent.putExtra("InentID", a);


    PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);             
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.notification)
            .setContentTitle("MY Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    builder.setContentIntent(contentIntent);
    Notification build = builder.getNotification();
    build.defaults |= Notification.DEFAULT_ALL;
    build.flags |= Notification.FLAG_AUTO_CANCEL;

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;
    mNotificationManager.notify(m, build);
    Log.d(TAG, "Notification sent successfully.");

}

这是我的Gamescreen活动的一部分

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_screen);



int i = getIntent().getExtras().getInt("InentID");

}

我在此活动中还有另外两个按钮。我想要做的是使用我的新数据重新启动此活动,同时我在这个Gamescreen活动中。当我在另一个活动中时,我的代码工作正常。请帮助我。我认为应该与Intent falg值有关。但我不知道怎么做

1 个答案:

答案 0 :(得分:0)

您必须使用此代码段

NotificationManager notificationManager = (NotificationManager) 

getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
        "A new notification", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = new Intent(this, NoficationDemoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString("buzz", "buzz");
intent.putExtras(bundle);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
        "This is the text", activity);
notification.number += 1;
notificationManager.notify(0, notification

希望这样对你有用:)