当用户在 Android 中点击通知时显示应用
Display app when user clicks on notification in Android
我已连接到 Google 云消息传递,并在收到消息时显示通知。
当用户点击通知时,我想 "maximize" 我的应用程序。 IE。显示与我的应用相关的最新 activity。或者,如果应用程序尚未启动,我想启动它的主要 activity。
如果应用程序已经打开,我必须不创建最后一个 activity 的新实例。
我怎样才能做到这一点?
我看过很多类似的问题,但所有答案似乎都想指定activity class,我不知道,因为我不知道哪个activity 上次显示。
这个看似简单的任务有解决方案吗?
我的代码目前看起来像这样:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("foo")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
但它不起作用。
当您在单击 Notification
的同时打开 Activity
时,只需打开以下 Activity
。即您的 PendingIntent
将在 Activity
之后打开
请阅读Activity
中写的所有comments
,这样你就会知道为什么要创建这个
public class NotificationHandlerActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//deep linking - resuming app code
if (isTaskRoot()) {
// This Activity is the only Activity, so
// the app wasn't running. So start the app from the
// beginning (redirect to MainActivity)
} else {
// App was already running, so just finish, which will drop the user
// in to the activity that was at the top of the task stack
Intent intent = getIntent();
Uri data = intent.getData();
//you can put your extra's if any
finish();
}
}
}
我已连接到 Google 云消息传递,并在收到消息时显示通知。 当用户点击通知时,我想 "maximize" 我的应用程序。 IE。显示与我的应用相关的最新 activity。或者,如果应用程序尚未启动,我想启动它的主要 activity。
如果应用程序已经打开,我必须不创建最后一个 activity 的新实例。
我怎样才能做到这一点?
我看过很多类似的问题,但所有答案似乎都想指定activity class,我不知道,因为我不知道哪个activity 上次显示。
这个看似简单的任务有解决方案吗?
我的代码目前看起来像这样:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("foo")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
但它不起作用。
当您在单击 Notification
的同时打开 Activity
时,只需打开以下 Activity
。即您的 PendingIntent
将在 Activity
请阅读Activity
中写的所有comments
,这样你就会知道为什么要创建这个
public class NotificationHandlerActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//deep linking - resuming app code
if (isTaskRoot()) {
// This Activity is the only Activity, so
// the app wasn't running. So start the app from the
// beginning (redirect to MainActivity)
} else {
// App was already running, so just finish, which will drop the user
// in to the activity that was at the top of the task stack
Intent intent = getIntent();
Uri data = intent.getData();
//you can put your extra's if any
finish();
}
}
}