下面列出了android.support.v4.app.TaskStackBuilder#addNextIntent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void sendBasicNotification(String notif) {
NotificationManager nm = (NotificationManager) StaticData.globalContext.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.alert_dark_frame;
CharSequence contentTitle = "TinyGSN notification";
CharSequence tickerText = notif;
long when = System.currentTimeMillis();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(StaticData.globalContext)
.setSmallIcon(icon)
.setContentTitle(contentTitle)
.setContentText(tickerText);
Intent notificationIntent = new Intent(StaticData.globalContext, ActivityViewData.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(StaticData.globalContext);
stackBuilder.addParentStack(ActivityViewData.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent contentIntent = PendingIntent.getActivity(StaticData.globalContext, 0,
notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
nm.notify(notify_id++, mBuilder.build());
}
public static Notification build(Context context,
NotificationCompat.Builder mBuilder, Intent resultIntent) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
return notification;
}
/**
* Displays a notification with the location results.
*/
void showNotification() {
Intent notificationIntent = new Intent(mContext, MainActivity.class);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder(mContext)
.setContentTitle(getLocationResultTitle())
.setContentText(getLocationResultText())
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(notificationPendingIntent);
getNotificationManager().notify(0, notificationBuilder.build());
}
private void sendNotification(String title, String body, String webUrl) {
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_stat_gcm)
.setContentTitle(title)
.setContentText(body);
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("web_url", webUrl);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
@NonNull
private TaskStackBuilder stackBuilderForNotificationTap(Message message) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
Bundle messageBundle = MessageBundleMapper.messageToBundle(message);
Class[] classes = propertyHelper().findClasses(MobileMessagingChatProperty.ON_MESSAGE_TAP_ACTIVITY_CLASSES);
if (classes != null) {
for (Class cls : classes) {
stackBuilder.addNextIntent(new Intent(context, cls)
.setAction(Event.NOTIFICATION_TAPPED.getKey())
.putExtras(messageBundle));
}
}
NotificationSettings notificationSettings = MobileMessagingCore.getInstance(context).getNotificationSettings();
if (stackBuilder.getIntentCount() == 0 && notificationSettings != null && notificationSettings.getCallbackActivity() != null) {
stackBuilder.addNextIntent(new Intent(context, notificationSettings.getCallbackActivity())
.setAction(Event.NOTIFICATION_TAPPED.getKey())
.putExtras(messageBundle));
}
return stackBuilder;
}
private void processDeepLink(Intent intent) {
if (intent == null || intent.getData() == null) {
return;
}
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
List<String> segments = intent.getData().getPathSegments();
if (segments.isEmpty()) {
Intent launcherIntent = getPackageManager().getLaunchIntentForPackage(getPackageName());
if (launcherIntent != null) {
startActivity(launcherIntent.putExtras(intent));
}
return;
}
for (String segment : intent.getData().getPathSegments()) {
if (activityMap.containsKey(segment)) {
Intent nextIntent = new Intent(this, activityMap.get(segment)).putExtras(intent);
stackBuilder.addNextIntent(nextIntent);
}
}
stackBuilder.startActivities();
}
private PendingIntent getPendingIntent() {
Intent intent = postActivity.getIntent();
intent.putExtra(postActivity.getString(R.string.post_intent_post_flag), FlagUnit.POST_RESEND);
intent.putExtra(postActivity.getString(R.string.post_intent_text), text);
intent.putExtra(postActivity.getString(R.string.post_intent_check_in), checkIn);
intent.putExtra(postActivity.getString(R.string.post_intent_picture_path), picturePath);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(postActivity);
taskStackBuilder.addParentStack(PostActivity.class);
taskStackBuilder.addNextIntent(intent);
return taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);
}
private PendingIntent getPendingIntent() {
Intent detailsIntent = new Intent(this, LoginActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(LoginActivity.class);
stackBuilder.addNextIntent(detailsIntent);
PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
return intent;
}
/**
* Displays a notification with the location results.
*/
void showNotification() {
Intent notificationIntent = new Intent(mContext, MainActivity.class);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder notificationBuilder = new Notification.Builder(mContext,
PRIMARY_CHANNEL)
.setContentTitle(getLocationResultTitle())
.setContentText(getLocationResultText())
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(notificationPendingIntent);
getNotificationManager().notify(0, notificationBuilder.build());
}
private void showDisconnectionNotification(String title, String message) {
String channel = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? "error" : "";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, channel)
.setPriority(PRIORITY_MAX)
.setSmallIcon(android.R.drawable.stat_notify_error)
.setContentTitle(title)
.setContentText(message)
.setTicker(message)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(USB_DISCONNECT_NOFICATION_ID, mBuilder.build());
}
public static void show(Context context, RemoteViews normal, RemoteViews expanded) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, PlaybackActivity.class);
// The stack builder will contain an artificial back stack for the started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(PlaybackActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
boolean isTransparent = Preferences.get(context).isNotificationIconTransparent();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = builder.setContent(normal)
.setWhen(0)
.setOngoing(true)
.setSmallIcon(isTransparent ? R.drawable.ic_transparent : R.drawable.ic_vlc_server)
.build();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
n.bigContentView = expanded;
}
notificationManager.notify(ID, n);
}
/**
* Requests permissions to be granted to this application.
*
* This method is a wrapper around
* {@link android.support.v4.app.ActivityCompat#requestPermissions(android.app.Activity, String[], int)}
* which works in a similar way, except it can be called from non-activity contexts. When called, it
* displays a notification with a customizable title and text. When the user taps the notification, an
* activity is launched in which the user is prompted to allow or deny the request.
*
* After the user has made a choice,
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])}
* is called, reporting whether the permissions were granted or not.
*
* @param context The context from which the request was made. The context supplied must implement
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback} and will receive the
* result of the operation.
* @param permissions The requested permissions
* @param requestCode Application specific request code to match with a result reported to
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])}
* @param notificationTitle The title for the notification
* @param notificationText The text for the notification
* @param notificationIcon Resource identifier for the notification icon
*/
public static <T extends Context & OnRequestPermissionsResultCallback> void requestPermissions(final T context, String[] permissions, int requestCode, String notificationTitle, String notificationText, int notificationIcon) {
ResultReceiver resultReceiver = new ResultReceiver(new Handler(Looper.getMainLooper())) {
@Override
protected void onReceiveResult (int resultCode, Bundle resultData) {
String[] outPermissions = resultData.getStringArray(Const.KEY_PERMISSIONS);
int[] grantResults = resultData.getIntArray(Const.KEY_GRANT_RESULTS);
context.onRequestPermissionsResult(resultCode, outPermissions, grantResults);
}
};
Intent permIntent = new Intent(context, PermissionRequestActivity.class);
permIntent.putExtra(Const.KEY_RESULT_RECEIVER, resultReceiver);
permIntent.putExtra(Const.KEY_PERMISSIONS, permissions);
permIntent.putExtra(Const.KEY_REQUEST_CODE, requestCode);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(permIntent);
PendingIntent permPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(notificationIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setOngoing(true)
//.setCategory(Notification.CATEGORY_STATUS)
.setAutoCancel(true)
.setWhen(0)
.setContentIntent(permPendingIntent)
.setStyle(null);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, builder.build());
}
/**
* The '@JavascriptInterface is required to make the method accessible from the Javascript
* layer
*
* The code in this method is based on the documentation here:
*
* http://developer.android.com/training/notify-user/build-notification.html
*
* @param message The message displayed in the notification
*/
@JavascriptInterface
public void showNotification(String message) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(mContext.getString(R.string.notification_title))
.setContentText(message)
.setAutoCancel(true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(mContext, MainActivity.class);
resultIntent.putExtra(MainFragment.EXTRA_FROM_NOTIFICATION, true);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(-1, mBuilder.build());
}
private void onRequestSuccess(List<GraphObject> list)
{
Log.d("FriendRequestService", "Num friend request : " + list.size());
Log.d("FriendRequestService", "Service : " + service.get());
if (service.get() == null)
return;
Service s = service.get();
if (list.size() > 0)
{
FriendRequest fq = (FriendRequest) list.get(0);
KlyphPreferences.setFriendRequestServiceOffset(fq.getTime());
final Builder builder = KlyphNotification.getBuilder(s, true);
builder.setContentTitle(fq.getUid_from_name()).setContentText(
s.getString(R.string.notification_friendrequest_message, fq.getUid_from_name()));
if (KlyphPreferences.mustGroupNotifications() && list.size() > 1)
{
sendNotification(list);
}
else
{
boolean isFirst = true;
for (GraphObject graphObject : list)
{
FriendRequest fr = (FriendRequest) graphObject;
TaskStackBuilder stackBuilder = TaskStackBuilder.create(service.get());
Intent intent = Klyph.getIntentForGraphObject(service.get(), fr);
// stackBuilder.addParentStack(UserActivity.class);
Intent mainIntent = new Intent(service.get(), MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
stackBuilder.addNextIntent(mainIntent);
stackBuilder.addNextIntent(intent);
int intentCode = (int) Math.round(Math.random() * 1000000);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(intentCode, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setContentTitle(fr.getUid_from_name());
builder.setContentText(s.getString(R.string.notification_friendrequest_message, fr.getUid_from_name()));
builder.setTicker(s.getString(R.string.notification_friendrequest_message, fr.getUid_from_name()));
if (isFirst == false)
{
KlyphNotification.setNoSound(builder);
KlyphNotification.setNoVibration(builder);
}
KlyphNotification.sendNotification(s, builder);
isFirst = false;
}
}
service.get().stopSelf();
}
else
{
s.stopSelf();
}
}
private void onRequestSuccess(List<GraphObject> list)
{
Log.d("BirthdayService", "onRequestSuccess " + list.size() + " " + service.get());
if (service.get() == null)
return;
Service s = service.get();
if (list.size() > 0)
{
final NotificationCompat.Builder builder = new NotificationCompat.Builder(s)
.setSmallIcon(R.drawable.ic_notification)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setDefaults(
android.app.Notification.DEFAULT_SOUND | android.app.Notification.DEFAULT_VIBRATE
| android.app.Notification.FLAG_ONLY_ALERT_ONCE);
final NotificationManager mNotificationManager = (NotificationManager) s.getSystemService(Context.NOTIFICATION_SERVICE);
if (KlyphPreferences.mustGroupNotifications() && list.size() > 1)
{
sendNotification(list);
}
else
{
boolean isFirst = true;
for (GraphObject graphObject : list)
{
Friend friend = (Friend) graphObject;
TaskStackBuilder stackBuilder = TaskStackBuilder.create(service.get());
Intent intent = Klyph.getIntentForGraphObject(service.get(), friend);
// stackBuilder.addParentStack(UserActivity.class);
Intent mainIntent = new Intent(service.get(), MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
stackBuilder.addNextIntent(mainIntent);
stackBuilder.addNextIntent(intent);
int intentCode = (int) Math.round(Math.random() * 1000000);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(intentCode, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
builder.setContentTitle(friend.getName());
builder.setContentText(s.getString(R.string.notification_birthday_today, friend.getName()));
builder.setTicker(s.getString(R.string.notification_birthday_today, friend.getName()));
if (isFirst == false)
{
builder.setDefaults(android.app.Notification.DEFAULT_VIBRATE | android.app.Notification.FLAG_ONLY_ALERT_ONCE);
builder.setSound(null);
}
final String tag = AttrUtil.getString(service.get(), R.string.app_name) + friend.getUid();
final int id = (int) System.currentTimeMillis();
mNotificationManager.notify(tag, id, builder.build());
isFirst = false;
}
}
}
s.stopSelf();
}
public static void showNotification(Context context, String message, MiPushMessage miPushMessage) {
if (BizLogic.isNotificationOn()) {
if (!BizLogic.isApplicationShowing(MainApp.CONTEXT) || MainApp.IS_SCREEN_LOCK) {
int numMessages = MainApp.PREF_UTIL.getInt(Constant.NOTIFICATION_COUNT, 0) + 1;
MainApp.PREF_UTIL.putInt(Constant.NOTIFICATION_COUNT, numMessages);
List<String> msgList = jsonToList(MainApp.PREF_UTIL.getString(Constant.NOTIFICATION_CONTENT,
""), String.class);
if (msgList == null) {
msgList = new ArrayList<>();
}
msgList.add(0, message);
MainApp.PREF_UTIL.putString(Constant.NOTIFICATION_CONTENT, listToJson(msgList));
/* Creates an explicit intent for an Activity in your app */
Intent resultIntent = new Intent(context, HomeActivity.class);
resultIntent.putExtra(PushMessageHelper.KEY_MESSAGE, miPushMessage);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.addCategory(CATEGORY_NOTIFICATION);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
/* Adds the Intent that starts the Activity to the top of the stack */
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(context.getString(R.string.app_name));
inboxStyle.setSummaryText(context.getString(R.string.new_message));
for (String mMsg : msgList) {
inboxStyle.addLine(mMsg);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(context.getString(R.string.app_name)) //设置通知栏标题
.setStyle(inboxStyle)
.setContentText(context.getString(R.string.new_message))
.setAutoCancel(true)
.setContentIntent(resultPendingIntent) //设置通知栏点击意图
.setNumber(numMessages) //设置通知集合的数量
.setWhen(System.currentTimeMillis()) //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS) //向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
.setSound(Uri.parse("android.resource://com.teambition.talk/" + R.raw.add), AudioManager.STREAM_NOTIFICATION)
.setSmallIcon(R.drawable.ic_notification); //设置通知小ICON
mNotificationManager.notify(Constant.NOTIFICATION_ID, mBuilder.build());
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
prefUnitType = mSharedPreferences.getBoolean(Const.KEY_PREF_UNIT_TYPE, prefUnitType);
prefKnots = mSharedPreferences.getBoolean(Const.KEY_PREF_KNOTS, prefKnots);
prefCoord = Integer.valueOf(mSharedPreferences.getString(Const.KEY_PREF_COORD, Integer.toString(prefCoord)));
mNotifyFix = mSharedPreferences.getBoolean(Const.KEY_PREF_NOTIFY_FIX, mNotifyFix);
mNotifySearch = mSharedPreferences.getBoolean(Const.KEY_PREF_NOTIFY_SEARCH, mNotifySearch);
if (mLocationManager.getAllProviders().indexOf(LocationManager.PASSIVE_PROVIDER) >= 0) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
requestLocationUpdates();
else
Log.w("PasvLocListenerService", "ACCESS_FINE_LOCATION permission not granted. Data display will not be available.");
} else {
Log.w("PasvLocListenerService", "No passive location provider found. Data display will not be available.");
}
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notify_location)
.setContentTitle(getString(R.string.value_none))
.setContentText(getString(R.string.value_none))
.setWhen(0)
.setVisibility(Notification.VISIBILITY_PUBLIC);
Intent mainIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(mainIntent);
PendingIntent mainPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(mainPendingIntent);
// if we were started through a broadcast, mGpsStatusReceiver had
// no way of picking it up, so we need to forward it manually
mGpsStatusReceiver.onReceive(this, intent);
return START_STICKY;
}
/**
* Displays an Android notification to the user, informing them that new messages
* have been received.<br><br>
*
* Note: This method is adapted from the example here: <br>
* https://developer.android.com/guide/topics/ui/notifiers/notifications.html
*
* @param title - A String containing the title of the notification to display
* @param text - A String containing the text of the notification to display
*/
private void displayNewMessagesNotification(String title, String text)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
builder.setSmallIcon(R.drawable.notification_icon_lollipop);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); // All we show is the number of new messages received
}
else
{
builder.setSmallIcon(R.drawable.notification_icon);
}
// Set a sound for the notification
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
// Creates an intent to open the Inbox Activity if the user selects the notification
Intent openNotificationIntent = new Intent(this, InboxActivity.class);
openNotificationIntent.putExtra(NotificationsService.EXTRA_NEW_MESSAGES_NOTIFICATION_CLEARED, true);
// Creates an intent to run the notifications service if the user clears the notification
Intent clearNotificationIntent = new Intent(getApplicationContext(), NotificationsService.class);
clearNotificationIntent.putExtra(NotificationsService.EXTRA_NEW_MESSAGES_NOTIFICATION_CLEARED, true);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, clearNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setDeleteIntent(pendingIntent);
// The stack builder object will contain an artificial back stack for the
// started Activity. This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(InboxActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(openNotificationIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The notification ID number allows you to update the notification later on.
notificationManager.notify(NEW_MESSAGES_NOTIFICATION_ID, builder.build());
}
/**
* Displays an Android notification to the user, informing them that the app
* needs to be unlocked (decrypting the database).
*
* Note: This method is adapted from the example here: <br>
* https://developer.android.com/guide/topics/ui/notifiers/notifications.html
*/
private void displayUnlockNotification()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(UNLOCK_NOTIFICATION_TITLE);
builder.setContentText(UNLOCK_NOTIFICATION_TEXT);
builder.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
builder.setSmallIcon(R.drawable.notification_icon_lollipop);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
}
else
{
builder.setSmallIcon(R.drawable.notification_icon);
}
// Creates an intent to open the lock screen activity if the user selects the notification
Intent openNotificationIntent = new Intent(this, LockScreenActivity.class);
openNotificationIntent.putExtra(NotificationsService.EXTRA_NEW_MESSAGES_NOTIFICATION_CLEARED, true);
// Creates an intent to run the notifications service if the user clears the notification
Intent clearNotificationIntent = new Intent(getApplicationContext(), NotificationsService.class);
clearNotificationIntent.putExtra(NotificationsService.EXTRA_NEW_MESSAGES_NOTIFICATION_CLEARED, true);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, clearNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setDeleteIntent(pendingIntent);
// The stack builder object will contain an artificial back stack for the
// started Activity. This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(LockScreenActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(openNotificationIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The notification ID number allows you to update the notification later on.
notificationManager.notify(UNLOCK_NOTIFICATION_ID, builder.build());
}
private void notifier(String title, String url, boolean isMessage, Bitmap picture) {
// let's display a notification, dude!
final String contentTitle;
if (isMessage)
contentTitle = getString(R.string.app_name) + ": " + getString(R.string.messages);
else
contentTitle = getString(R.string.app_name) + ": " + getString(R.string.notifications);
// log line (show what type of notification is about to be displayed)
Log.i(TAG, "Start notification - isMessage: " + isMessage);
// start building a notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setSmallIcon(R.mipmap.ic_stat_fs)
.setContentTitle(contentTitle)
.setContentText(title)
.setTicker(title)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
// picture is available
if (picture != null)
mBuilder.setLargeIcon(picture);
// ringtone
String ringtoneKey = "ringtone";
if (isMessage)
ringtoneKey = "ringtone_msg";
Uri ringtoneUri = Uri.parse(preferences.getString(ringtoneKey, "content://settings/system/notification_sound"));
mBuilder.setSound(ringtoneUri);
// vibration
if (preferences.getBoolean("vibrate", false))
mBuilder.setVibrate(new long[] {500, 500});
else
mBuilder.setVibrate(new long[] {0L});
// LED light
if (preferences.getBoolean("led_light", false)) {
Resources resources = getResources(), systemResources = Resources.getSystem();
mBuilder.setLights(Color.CYAN,
resources.getInteger(systemResources.getIdentifier("config_defaultNotificationLedOn", "integer", "android")),
resources.getInteger(systemResources.getIdentifier("config_defaultNotificationLedOff", "integer", "android")));
}
// priority for Heads-up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mBuilder.setPriority(Notification.PRIORITY_HIGH);
// intent with notification url in extra
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("start_url", url);
intent.setAction("NOTIFICATION_URL_ACTION");
// final notification building
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(false);
Notification note = mBuilder.build();
// LED light flag
if (preferences.getBoolean("led_light", false))
note.flags |= Notification.FLAG_SHOW_LIGHTS;
// display a notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// because message notifications are displayed separately
if (isMessage)
mNotificationManager.notify(1, note);
else
mNotificationManager.notify(0, note);
}