2

私はプッシュ通知で働いています。ユーザーが通知をクリックしたときに、アプリケーションが開始されていない場合はアプリケーションを開始するか、開始されている場合はアプリケーションを前面に表示します。ありがとう

4

5 に答える 5

1

これは複雑なコードです ここで答えを見つけましたユーザーがホームボタンをクリックした後にアプリケーションを前面に表示します

    Intent intent = new Intent(ctx, SplashScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
                    PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    ctx).setContentTitle(extras.getString("title"))
                    .setContentText(extras.getString("message"))
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(contentIntent);
           Notification noti = mBuilder.build();
           noti.flags = Notification.DEFAULT_LIGHTS
                | Notification.FLAG_AUTO_CANCEL;
           mNotificationManager.notify(NOTIFICATION_ID, noti);

重要なのはインテントのフラグです。これにより、アプリが開くか、開いている場合は前面に表示されます。アプリの閲覧中に通知をクリックしても何もしません

于 2013-07-09T13:14:47.867 に答える
0

通知のインテントを設定するだけです。これについては、公式 API ガイドで詳しく説明されています。

于 2013-07-09T13:00:14.210 に答える
0

を作成することでそれを行いますPendingIntent
たとえば、これは通知がクリックされると MainActivity を起動します。

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification noti = new Notification.Builder(this)
        .setContentTitle("Notication title")
        .setContentText("Content text")
        .setContentIntent(pendingIntent).build();
于 2013-07-09T13:01:18.930 に答える
0

Google Cloud Messaging について話しているのかどうかはわかりません。ただし、通常の通知の場合は、通知の作成中に Pending Intent を提供する必要があります。ユーザーがその通知をクリックすると、いわゆるアプリケーションに移動するように、目的のクラスを Pending インテントに入れるだけです。スニペット:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
        "Your application activity".class), PendingIntent."Flag you want");

この後、通知の作成中にこのインテントを使用します。そのメソッドは setContentIntent("above intent") であり、NotificationManager で通知を発生させます!

于 2013-07-09T13:12:08.850 に答える