下面列出了android.app.PendingIntent#getActivities ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void sendNotificationForBack2() {
Intent backIntent = new Intent(this, MainActivity.class);
backIntent.putExtra("from", "sendNotificationForBack2");
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent intent = new Intent(this, SecondActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{backIntent, intent}, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("from SecondActivity");
builder.setContentText("test notification press back go main activity");
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, builder.build());
}
/**
* Creates the notification
*
* @param messageResId message resource id. The message must have one String parameter,<br />
* f.e. <code><string name="name">%s is connected</string></code>
* @param defaults
*/
@SuppressWarnings("SameParameterValue")
private Notification createNotification(final int messageResId, final int defaults) {
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Intent targetIntent = new Intent(this, RSCActivity.class);
final Intent disconnect = new Intent(ACTION_DISCONNECT);
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
// both activities above have launchMode="singleTask" in the AndroidManifest.xml file, so if the task is already running, it will be resumed
final PendingIntent pendingIntent = PendingIntent.getActivities(this, OPEN_ACTIVITY_REQ, new Intent[]{parentIntent, targetIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
builder.setSmallIcon(R.drawable.ic_stat_notify_rsc);
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.rsc_notification_action_disconnect), disconnectAction));
return builder.build();
}
/**
* Creates the notification
*
* @param messageResId message resource id. The message must have one String parameter,<br />
* f.e. <code><string name="name">%s is connected</string></code>
* @param defaults signals that will be used to notify the user
*/
@SuppressWarnings("SameParameterValue")
protected Notification createNotification(final int messageResId, final int defaults) {
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Intent targetIntent = new Intent(this, UARTActivity.class);
final Intent disconnect = new Intent(ACTION_DISCONNECT);
disconnect.putExtra(EXTRA_SOURCE, SOURCE_NOTIFICATION);
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
// both activities above have launchMode="singleTask" in the AndroidManifest.xml file, so if the task is already running, it will be resumed
final PendingIntent pendingIntent = PendingIntent.getActivities(this, OPEN_ACTIVITY_REQ, new Intent[]{parentIntent, targetIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
builder.setSmallIcon(R.drawable.ic_stat_notify_uart);
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.uart_notification_action_disconnect), disconnectAction));
return builder.build();
}
/**
* Creates the notification.
*
* @param messageResId message resource id. The message must have one String parameter,<br />
* f.e. <code><string name="name">%s is connected</string></code>
* @param defaults signals that will be used to notify the user
*/
@SuppressWarnings("SameParameterValue")
private Notification createNotification(final int messageResId, final int defaults) {
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Intent targetIntent = new Intent(this, TemplateActivity.class);
final Intent disconnect = new Intent(ACTION_DISCONNECT);
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
// both activities above have launchMode="singleTask" in the AndroidManifest.xml file, so if the task is already running, it will be resumed
final PendingIntent pendingIntent = PendingIntent.getActivities(this, OPEN_ACTIVITY_REQ, new Intent[]{parentIntent, targetIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
builder.setSmallIcon(R.drawable.ic_stat_notify_template);
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.template_notification_action_disconnect), disconnectAction));
return builder.build();
}
/**
* Creates the notification
*
* @param messageResId the message resource id. The message must have one String parameter,<br />
* f.e. <code><string name="name">%s is connected</string></code>
* @param defaults
*/
@SuppressWarnings("SameParameterValue")
private Notification createNotification(final int messageResId, final int defaults) {
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Intent targetIntent = new Intent(this, CSCActivity.class);
final Intent disconnect = new Intent(ACTION_DISCONNECT);
final PendingIntent disconnectAction = PendingIntent.getBroadcast(this, DISCONNECT_REQ, disconnect, PendingIntent.FLAG_UPDATE_CURRENT);
// both activities above have launchMode="singleTask" in the AndroidManifest.xml file, so if the task is already running, it will be resumed
final PendingIntent pendingIntent = PendingIntent.getActivities(this, OPEN_ACTIVITY_REQ, new Intent[]{parentIntent, targetIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.CONNECTED_DEVICE_CHANNEL);
builder.setContentIntent(pendingIntent);
builder.setContentTitle(getString(R.string.app_name)).setContentText(getString(messageResId, getDeviceName()));
builder.setSmallIcon(R.drawable.ic_stat_notify_csc);
builder.setShowWhen(defaults != 0).setDefaults(defaults).setAutoCancel(true).setOngoing(true);
builder.addAction(new NotificationCompat.Action(R.drawable.ic_action_bluetooth, getString(R.string.csc_notification_action_disconnect), disconnectAction));
return builder.build();
}
private NotificationItem(int id, String title, String desc, String channelId) {
super(id, title, desc);
final Intent[] intents = new Intent[2];
intents[0] = Intent.makeMainActivity(
new ComponentName(DemoApplication.CONTEXT, MainActivity.class));
intents[1] = new Intent(DemoApplication.CONTEXT, NotificationSampleActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivities(
DemoApplication.CONTEXT, 0, intents,
PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(
FileDownloadHelper.getAppContext(),
channelId);
} else {
//noinspection deprecation
builder = new NotificationCompat.Builder(FileDownloadHelper.getAppContext())
.setDefaults(Notification.DEFAULT_LIGHTS)
.setPriority(NotificationCompat.PRIORITY_MIN);
}
builder.setContentTitle(getTitle())
.setContentText(desc)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher);
}
private void sendNotification(Channel channel, String notificationTitle, String notificationBody, Bitmap bitmap, Intent intent, Intent backIntent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent;
if (backIntent != null) {
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent[] intents = new Intent[]{backIntent, intent};
pendingIntent = PendingIntent.getActivities(this, notificationId++, intents, PendingIntent.FLAG_ONE_SHOT);
} else {
pendingIntent = PendingIntent.getActivity(this, notificationId++, intent, PendingIntent.FLAG_ONE_SHOT);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel.id);
notificationBuilder.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.drawable.ic_push_notification_small) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setLargeIcon(bitmap)
.setSound(defaultSoundUri);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channel.id, getString(channel.name), importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(ContextCompat.getColor(this, R.color.primary));
notificationChannel.enableVibration(true);
notificationBuilder.setChannelId(channel.id);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationId++, notificationBuilder.build());
}
private void sendNotification(Channel channel, String notificationTitle, String notificationBody, Bitmap bitmap, Intent intent, Intent backIntent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent;
if (backIntent != null) {
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent[] intents = new Intent[]{backIntent, intent};
pendingIntent = PendingIntent.getActivities(this, notificationId++, intents, PendingIntent.FLAG_ONE_SHOT);
} else {
pendingIntent = PendingIntent.getActivity(this, notificationId++, intent, PendingIntent.FLAG_ONE_SHOT);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel.id);
notificationBuilder.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.drawable.ic_push_notification_small) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setLargeIcon(bitmap)
.setSound(defaultSoundUri);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channel.id, getString(channel.name), importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(ContextCompat.getColor(this, R.color.primary));
notificationChannel.enableVibration(true);
notificationBuilder.setChannelId(channel.id);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationId++, notificationBuilder.build());
}
private void createNotification() {
if(mNearbyDevicesMessageList.size() == 0){
mNotificationManager.cancelAll();
return;
}
final ArrayList<Message> nearbyMessageList = loadNearbyMessageListForNotification();
mParentIntent.putExtra(NEARBY_DEVICE_DATA, nearbyMessageList);
mParentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mPendingIntent = PendingIntent.getActivities(getActivity(), OPEN_ACTIVITY_REQ, new Intent[]{mParentIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.ic_eddystone)
.setColor(ContextCompat.getColor(getActivity(), R.color.actionBarColor))
.setContentTitle(getString(R.string.app_name))
.setContentIntent(mPendingIntent);
if(mNearbyDevicesMessageList.size() == 1) {
mBuilder.setContentText(new String(mNearbyDevicesMessageList.get(0).getContent(), Charset.forName("UTF-8")));
} else {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(getString(R.string.app_name));
inboxStyle.setSummaryText(mNearbyDevicesMessageList.size() + " beacons found");
mBuilder.setContentText(mNearbyDevicesMessageList.size() + " beacons found");
for (int i = 0; i < mNearbyDevicesMessageList.size(); i++) {
inboxStyle.addLine(new String(mNearbyDevicesMessageList.get(i).getContent(), Charset.forName("UTF-8")));
}
mBuilder.setStyle(inboxStyle);
}
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
/**
* 显示通知
*
* @param comicChapter
*/
public void showNotification(DownloadManagerService service, ComicChapter comicChapter) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
//统一设置
builder.setSmallIcon(android.R.drawable.stat_sys_download)
.setLargeIcon(mBitmap)
.setAutoCancel(false)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_PROGRESS);
builder.setTicker(comicChapter.getChapterName() + " 开始下载");
//设置通知消息
CharSequence contentTitle = comicChapter.getChapterName() + " - 开始下载"; // 通知栏标题
CharSequence contentText = 0 + "/" + comicChapter.getPageCount(); // 通知栏内容
//设置跳转到主界面
Intent intent0 = new Intent(mContext, MainActivity.class);
//设置点击通知栏之后的操作
Intent intent1 = new Intent(mContext, DownloadingChapterActivity.class);
//封装到一起
Intent[] intents = {intent0, intent1};
//封装到PendingIntent中
PendingIntent contentIntent = PendingIntent.getActivities(mContext, 0, intents, 0);
//统一设置
builder.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(contentIntent);
builder.setProgress(comicChapter.getPageCount(), 0, true); //false为带刻度的进度条,true不明确进度
Notification notification = builder.build();
//发出通知,将service设为前台
service.startForeground(comicChapter.getId(), notification);
}
/**
* 通知漫画下载完毕
*
* @param comicChapter
*/
public void finishedNotification(ComicChapter comicChapter) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
//小图标及滑动文字
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setLargeIcon(mBitmap)
.setTicker(comicChapter.getChapterName() + " 下载完毕")
.setOngoing(false)
.setAutoCancel(true);
//设置通知消息
CharSequence contentTitle = comicChapter.getComicTitle(); // 通知栏标题
CharSequence contentText = comicChapter.getChapterName() + " - 下载完毕"; // 通知栏内容
//设置跳转到主界面
Intent intent0 = new Intent(mContext, MainActivity.class);
//设置点击通知栏之后的操作
Intent intent1 = new Intent(mContext, OfflineComicDownloadActivity.class);
//封装到一起
Intent[] intents = {intent0, intent1};
//封装到PendingIntent中
PendingIntent contentIntent = PendingIntent.getActivities(mContext, 0, intents, 0);
//统一设置
builder.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(contentIntent);
//设置点击通知栏的内容后会自动消失
builder.setAutoCancel(true);
Notification notification = builder.build();
//发出通知
sNotificationManager.notify(Constants.FINISHED_NOTIFICATION_ID, notification);
}
private NotificationCompat.Builder getNotificationBuilder() {
final Intent parentIntent = new Intent(this, FeaturesActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final Intent targetIntent = new Intent(this, ProximityActivity.class);
// Both activities above have launchMode="singleTask" in the AndroidManifest.xml file, so if the task is already running, it will be resumed
final PendingIntent pendingIntent = PendingIntent.getActivities(this, OPEN_ACTIVITY_REQ, new Intent[]{parentIntent, targetIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ToolboxApplication.PROXIMITY_WARNINGS_CHANNEL);
builder.setContentIntent(pendingIntent).setAutoCancel(false);
builder.setSmallIcon(R.drawable.ic_stat_notify_proximity);
return builder;
}
private PendingIntent makeDefaultIntent() {
// A typical convention for notifications is to launch the user deeply
// into an application representing the data in the notification; to
// accomplish this, we can build an array of intents to insert the back
// stack stack history above the item being displayed.
Intent[] intents = new Intent[4];
// First: root activity of ApiDemos.
// This is a convenient way to make the proper Intent to launch and
// reset an application's task.
intents[0] = Intent.makeRestartActivityTask(new ComponentName(this,
com.example.android.apis.ApiDemos.class));
// "App"
intents[1] = new Intent(this, com.example.android.apis.ApiDemos.class);
intents[1].putExtra("com.example.android.apis.Path", "App");
// "App/Notification"
intents[2] = new Intent(this, com.example.android.apis.ApiDemos.class);
intents[2].putExtra("com.example.android.apis.Path", "App/Notification");
// Now the activity to display to the user.
intents[3] = new Intent(this, StatusBarNotifications.class);
// The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_UPDATE_CURRENT so that if there
// is already an active matching pending intent, we will update its
// extras (and other Intents in the array) to be the ones passed in here.
PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
intents, PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent;
}
public static PendingIntent getActivitiesPendingIntent(Context context, int requestCode,
Intent[] intents, int flags, Bundle options) {
return PendingIntent.getActivities(context, requestCode, intents, flags, options);
}
@Override
public void onReceive(Context context, Intent intent) {
boolean shouldNotify = DefaultPrefs.get(context).getNotificationFlag();
if (!shouldNotify) {
return;
}
Session session = Parcels.unwrap(intent.getParcelableExtra(Session.class.getSimpleName()));
// launch SessionDetailsActivity stacked with MainActivity
Intent sessionDetailIntent = SessionDetailActivity.createIntent(context, session);
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Intent[] intents = {mainIntent, sessionDetailIntent};
PendingIntent pendingIntent = PendingIntent.getActivities(context, 0, intents, PendingIntent.FLAG_CANCEL_CURRENT);
Bitmap appIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
String title = context.getResources().getQuantityString(
R.plurals.schedule_notification_title, REMIND_MINUTES, REMIND_MINUTES);
boolean headsUp = DefaultPrefs.get(context).getHeadsUpFlag();
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(appIcon)
.setContentTitle(title)
.setContentText(session.title)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(headsUp ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_EVENT)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setColor(ContextCompat.getColor(context, R.color.theme500))
.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
// Using fixed ID to avoid to show multiple notifications
manager.notify(0, notification);
}
public static PendingIntent getActivitiesPendingIntent(Context context, int requestCode,
Intent[] intents, int flags, Bundle options) {
return PendingIntent.getActivities(context, requestCode, intents, flags, options);
}
public static PendingIntent getActivitiesPendingIntent(Context context, int requestCode,
Intent[] intents, int flags) {
return PendingIntent.getActivities(context, requestCode, intents, flags);
}
public static PendingIntent getActivitiesPendingIntent(Context context, int requestCode,
Intent[] intents, int flags) {
return PendingIntent.getActivities(context, requestCode, intents, flags);
}
public static PendingIntent a(Context context, int i, Intent aintent[], int j, Bundle bundle)
{
return PendingIntent.getActivities(context, i, aintent, j, bundle);
}
public static PendingIntent getActivitiesPendingIntent(Context context, int requestCode,
Intent[] intents, int flags) {
return PendingIntent.getActivities(context, requestCode, intents, flags);
}