下面列出了android.app.Notification#DEFAULT_SOUND 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Uri calculateSound() {
final Notification n = sbn.getNotification();
// No notification sounds on tv
if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
return null;
}
Uri sound = mChannel.getSound();
if (mPreChannelsNotification && (getChannel().getUserLockedFields()
& NotificationChannel.USER_LOCKED_SOUND) == 0) {
final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
if (useDefaultSound) {
sound = Settings.System.DEFAULT_NOTIFICATION_URI;
} else {
sound = n.sound;
}
}
return sound;
}
public void addCustomNotification(Intent onClickIntent, int iconResourceID, String title, String message, boolean isOngoingEvent) {
PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap bm = null;
try {
bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
} catch (Exception e) {
}
mCustomNotif = Compatibility.createNotification(this, title, message, iconResourceID, 0, bm, notifContentIntent, isOngoingEvent,notifcationsPriority);
mCustomNotif.defaults |= Notification.DEFAULT_VIBRATE;
mCustomNotif.defaults |= Notification.DEFAULT_SOUND;
mCustomNotif.defaults |= Notification.DEFAULT_LIGHTS;
notifyWrapper(CUSTOM_NOTIF_ID, mCustomNotif);
}
@SuppressWarnings("deprecation")
public static void showNotification(Context context, int icon, String text) {
final Notification notifyDetails = new Notification(icon, text, System.currentTimeMillis());
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT;
if (prefsSound(context)) {
notifyDetails.defaults |= Notification.DEFAULT_SOUND;
}
if (prefsVibrate(context)) {
notifyDetails.defaults |= Notification.DEFAULT_VIBRATE;
}
Intent notifyIntent = new Intent(context, adbWireless.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notifyIntent, 0);
notifyDetails.setLatestEventInfo(context, context.getResources().getString(R.string.noti_title), text, intent);
if (Utils.mNotificationManager != null) {
Utils.mNotificationManager.notify(Utils.START_NOTIFICATION_ID, notifyDetails);
} else {
Utils.mNotificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
}
Utils.mNotificationManager.notify(Utils.START_NOTIFICATION_ID, notifyDetails);
}
private void startServiceInternal() {
setNotifyChannel();
Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
// 设置PendingIntent
Intent nfIntent = new Intent(this, MainActivity.class);
builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, FLAG_UPDATE_CURRENT))
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
.setContentTitle("G4Proxy") // 设置下拉列表里的标题
.setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
.setContentText("代理服务agent") // 设置上下文内容
.setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(BuildConfig.APPLICATION_ID);
}
Notification notification = builder.build(); // 获取构建好的Notification
notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
startForeground(110, notification);// 开始前台服务
String clientKey = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
//新的代理服务器实现
LogbackConfig.config();
G4ProxyClient g4ProxyClient = new G4ProxyClient("www.scumall.com", 50000, clientKey);
g4ProxyClient.startup();
}
public static void nmNotifyDefault(NotificationManager nm, Context context,
int notifyId, Intent intent, String title,
String contentText,
int iconId) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setSmallIcon(iconId);
builder.setContentText(contentText);
builder.setContentTitle(title);
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT));
builder.setWhen(System.currentTimeMillis());
Notification notification = null;
notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL
| Notification.FLAG_ONLY_ALERT_ONCE;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledARGB = 0xFF84E4FA;
notification.ledOnMS = 3000;
notification.ledOffMS = 2000;
nm.notify(notifyId, notification);
}
private static void applyStyle (final Builder nb, final NotificationStyle ns) {
int defaults = 0;
if (ns.isLights()) defaults |= Notification.DEFAULT_LIGHTS;
if (ns.isVibrate()) defaults |= Notification.DEFAULT_VIBRATE;
if (ns.isSound()) defaults |= Notification.DEFAULT_SOUND;
nb.setDefaults(defaults);
}
@SuppressLint("NewApi")
protected void showNotify(String title, String content, String info, Class<?> jumpClass) {
if(!Conf.isNotify) return;
Intent intent = new Intent(mContext, jumpClass);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,intent,0);
//获得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
Notification notification = new Notification.Builder(mContext)
.setContentText(content)
.setContentTitle(title)
.setContentInfo(info)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL; // 自动销毁
notification.defaults |= Notification.DEFAULT_LIGHTS;
if(Conf.isNotifySound) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if(Conf.isNotifyVibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
manager.notify(0, notification);//发动通知,id由自己指定,每一个Notification对应的唯一标志
}
private Notification createNotification(String message) {
Notification notification = new Notification();
notification.icon = getNotificationIcon();
notification.defaults = Notification.DEFAULT_LIGHTS;
if (isNotificationSoundEnabled()) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (isNotificationVibrateEnabled()) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.when = System.currentTimeMillis();
notification.tickerText = message;
return notification;
}
/**
* 设置builder的信息,在用大文本时会用到这个
*
* @param pendingIntent
* @param smallIcon
* @param ticker
*/
private void setBuilder(PendingIntent pendingIntent, int smallIcon, String ticker, boolean sound, boolean vibrate, boolean lights) {
nBuilder = new Notification.Builder(mContext);
// 如果当前Activity启动在前台,则不开启新的Activity。
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// PendingIntent pIntent = PendingIntent.getActivity(mContext,
// requestCode, intent, FLAG);
nBuilder.setContentIntent(pendingIntent);
nBuilder.setSmallIcon(smallIcon);
nBuilder.setTicker(ticker);
nBuilder.setWhen(System.currentTimeMillis());
nBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
int defaults = 0;
if (sound) {
defaults |= Notification.DEFAULT_SOUND;
}
if (vibrate) {
defaults |= Notification.DEFAULT_VIBRATE;
}
if (lights) {
defaults |= Notification.DEFAULT_LIGHTS;
}
nBuilder.setDefaults(defaults);
}
public void countApiUse(NotificationRecord record) {
final Notification n = record.getNotification();
if (n.actions != null) {
numWithActions++;
}
if ((n.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) {
numForegroundService++;
}
if ((n.flags & Notification.FLAG_ONGOING_EVENT) != 0) {
numOngoing++;
}
if ((n.flags & Notification.FLAG_AUTO_CANCEL) != 0) {
numAutoCancel++;
}
if ((n.defaults & Notification.DEFAULT_SOUND) != 0 ||
(n.defaults & Notification.DEFAULT_VIBRATE) != 0 ||
n.sound != null || n.vibrate != null) {
numInterrupt++;
}
switch (n.visibility) {
case Notification.VISIBILITY_PRIVATE:
numPrivate++;
break;
case Notification.VISIBILITY_SECRET:
numSecret++;
break;
}
if (record.stats.isNoisy) {
noisyImportance.increment(record.stats.requestedImportance);
} else {
quietImportance.increment(record.stats.requestedImportance);
}
finalImportance.increment(record.getImportance());
final Set<String> names = n.extras.keySet();
if (names.contains(Notification.EXTRA_BIG_TEXT)) {
numWithBigText++;
}
if (names.contains(Notification.EXTRA_PICTURE)) {
numWithBigPicture++;
}
if (names.contains(Notification.EXTRA_LARGE_ICON)) {
numWithLargeIcon++;
}
if (names.contains(Notification.EXTRA_TEXT_LINES)) {
numWithInbox++;
}
if (names.contains(Notification.EXTRA_MEDIA_SESSION)) {
numWithMediaSession++;
}
if (names.contains(Notification.EXTRA_TITLE) &&
!TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_TITLE))) {
numWithTitle++;
}
if (names.contains(Notification.EXTRA_TEXT) &&
!TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_TEXT))) {
numWithText++;
}
if (names.contains(Notification.EXTRA_SUB_TEXT) &&
!TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_SUB_TEXT))) {
numWithSubText++;
}
if (names.contains(Notification.EXTRA_INFO_TEXT) &&
!TextUtils.isEmpty(n.extras.getCharSequence(Notification.EXTRA_INFO_TEXT))) {
numWithInfoText++;
}
}
public Sound(NotificationSignal original)
{
super(new Toggled(Notification.DEFAULT_SOUND, true, original));
}
public void showNotification(JSONObject dataToReturnOnClick, JSONObject notificationOptions){
Options options = new Options(notificationOptions, this.context.getApplicationContext());
this.createNotificationChannel(options);
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new Notification.Builder(this.context, options.getChannelId());
} else {
builder = new Notification.Builder(this.context);
}
builder.setDefaults(0)
.setContentTitle(options.getTitle()).setSmallIcon(options.getSmallIconResourceId())
.setLargeIcon(options.getLargeIconBitmap()).setAutoCancel(options.doesAutoCancel());
if(options.getBigPictureBitmap() != null)
builder.setStyle(new BigPictureStyle().bigPicture(options.getBigPictureBitmap()));
if(options.doesVibrate() && options.getVibratePattern() != null)
builder.setVibrate(options.getVibratePattern());
else if (Build.VERSION.SDK_INT >= 21 && options.doesHeadsUp())
builder.setVibrate(new long[0]);
if(options.doesHeadsUp()) {
builder.setPriority(Notification.PRIORITY_HIGH);
}
if(options.doesSound() && options.getSoundUri() != null)
builder.setSound(options.getSoundUri(), android.media.AudioManager.STREAM_NOTIFICATION);
if (options.doesColor() && Build.VERSION.SDK_INT >= 22)
builder.setColor(options.getColor());
this.setContentTextAndMultiline(builder, options);
this.setOnClick(builder, dataToReturnOnClick);
Notification notification;
if (Build.VERSION.SDK_INT < 16) {
notification = builder.getNotification(); // Notification for HoneyComb to ICS
} else {
notification = builder.build(); // Notification for Jellybean and above
}
if(options.doesAutoCancel())
notification.flags |= Notification.FLAG_AUTO_CANCEL;
if(options.doesVibrate() && options.getVibratePattern() == null)
notification.defaults |= Notification.DEFAULT_VIBRATE;
if(options.doesSound() && options.getSoundUri() == null)
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager
= (NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(options.getId(), notification);
if(options.doesOpenApp())
openApp();
}
@Override
public void onHandleIntent(Intent intent) {
Log.d(TAG, "service start");
if (BaseApi.getAccessToken() == null) {
BaseApi.setAccessToken(new LoginApiCache(this).getAccessToken());
if (BaseApi.getAccessToken() == null) {
return;
}
}
CommentTimeLineApiCache cache = new CommentTimeLineApiCache(this);
cache.loadFromCache();
if (cache.mMessages.getSize() > 0) {
CommentModel last = (CommentModel) cache.mMessages.get(0);
CommentListModel since = CommentTimeLineApi.fetchCommentTimeLineSince(last.id);
if (since != null && since.getSize() > 0) {
cache.mMessages.addAll(true, since);
cache.cache();
int size = since.getSize();
String str = String.format(getResources().getString(R.string.new_comment), size);
Settings settings = Settings.getInstance(getApplicationContext());
int defaults = (settings.getBoolean(Settings.NOTIFICATION_SOUND, true) ? Notification.DEFAULT_SOUND : 0)|
(settings.getBoolean(Settings.NOTIFICATION_VIBRATE, true) ? Notification.DEFAULT_VIBRATE : 0)|
Notification.DEFAULT_LIGHTS;
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, EntryActivity.class), 0);
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle(str)
.setContentText(getString(R.string.click_to_view))
.setSmallIcon(R.drawable.ic_action_chat)
.setDefaults(defaults)
.setAutoCancel(true)
.setContentIntent(i)
.build();
NotificationManager m = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
m.notify(ID, n);
}
}
Log.d(TAG, "service finished");
}
public void notify(String notificationId, String apiKey, String title,
String message, String uri) {
Log.d(LOGTAG, "notify()...");
Log.d(LOGTAG, "notificationId=" + notificationId);
Log.d(LOGTAG, "notificationApiKey=" + apiKey);
Log.d(LOGTAG, "notificationTitle=" + title);
Log.d(LOGTAG, "notificationMessage=" + message);
Log.d(LOGTAG, "notificationUri=" + uri);
if (isNotificationEnabled()) {
// Show the toast
if (isNotificationToastEnabled()) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
// Notification
Notification notification = new Notification();
notification.icon = getNotificationIcon();
notification.defaults = Notification.DEFAULT_LIGHTS;
if (isNotificationSoundEnabled()) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (isNotificationVibrateEnabled()) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.when = System.currentTimeMillis();
notification.tickerText = message;
// Intent intent;
// if (uri != null
// && uri.length() > 0
// && (uri.startsWith("http:") || uri.startsWith("https:")
// || uri.startsWith("tel:") || uri.startsWith("geo:"))) {
// intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
// } else {
// String callbackActivityPackageName = sharedPrefs.getString(
// Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");
// String callbackActivityClassName = sharedPrefs.getString(
// Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
// intent = new Intent().setClassName(callbackActivityPackageName,
// callbackActivityClassName);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// }
Intent intent = new Intent(context,
NotificationDetailsActivity.class);
intent.putExtra(Constants.NOTIFICATION_ID, notificationId);
intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
intent.putExtra(Constants.NOTIFICATION_TITLE, title);
intent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
intent.putExtra(Constants.NOTIFICATION_URI, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message,
contentIntent);
notificationManager.notify(random.nextInt(), notification);
// Intent clickIntent = new Intent(
// Constants.ACTION_NOTIFICATION_CLICKED);
// clickIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
// clickIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
// clickIntent.putExtra(Constants.NOTIFICATION_TITLE, title);
// clickIntent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
// clickIntent.putExtra(Constants.NOTIFICATION_URI, uri);
// // positiveIntent.setData(Uri.parse((new StringBuilder(
// // "notif://notification.adroidpn.org/")).append(apiKey).append(
// // "/").append(System.currentTimeMillis()).toString()));
// PendingIntent clickPendingIntent = PendingIntent.getBroadcast(
// context, 0, clickIntent, 0);
//
// notification.setLatestEventInfo(context, title, message,
// clickPendingIntent);
//
// Intent clearIntent = new Intent(
// Constants.ACTION_NOTIFICATION_CLEARED);
// clearIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
// clearIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
// // negativeIntent.setData(Uri.parse((new StringBuilder(
// // "notif://notification.adroidpn.org/")).append(apiKey).append(
// // "/").append(System.currentTimeMillis()).toString()));
// PendingIntent clearPendingIntent = PendingIntent.getBroadcast(
// context, 0, clearIntent, 0);
// notification.deleteIntent = clearPendingIntent;
//
// notificationManager.notify(random.nextInt(), notification);
} else {
Log.w(LOGTAG, "Notificaitons disabled.");
}
}
public void build(){
cBuilder = new NotificationCompat.Builder(NotifyUtil.context);
cBuilder.setContentIntent(contentIntent);// 该通知要启动的Intent
if(smallIcon >0){
cBuilder.setSmallIcon(smallIcon);// 设置顶部状态栏的小图标
}
if(bigIcon >0){
cBuilder.setLargeIcon(BitmapFactory.decodeResource(NotifyUtil.context.getResources(), bigIcon));
}
cBuilder.setTicker(ticker);// 在顶部状态栏中的提示信息
cBuilder.setContentTitle(contentTitle);// 设置通知中心的标题
if(!TextUtils.isEmpty(contentText)){
cBuilder.setContentText(contentText);// 设置通知中心中的内容
}
cBuilder.setWhen(System.currentTimeMillis());
//cBuilder.setStyle()
/*
* 将AutoCancel设为true后,当你点击通知栏的notification后,它会自动被取消消失,
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
cBuilder.setAutoCancel(true);
// 将Ongoing设为true 那么notification将不能滑动删除
// notifyBuilder.setOngoing(true);
/*
* 从Android4.1开始,可以通过以下方法,设置notification的优先级,
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
cBuilder.setPriority(priority);
//int defaults = 0;
if (sound) {
defaults |= Notification.DEFAULT_SOUND;
}
if (vibrate) {
defaults |= Notification.DEFAULT_VIBRATE;
}
if (lights) {
defaults |= Notification.DEFAULT_LIGHTS;
}
cBuilder.setDefaults(defaults);
//按钮
if(btnActionBeens!=null && btnActionBeens.size()>0){
for(BtnActionBean bean: btnActionBeens){
cBuilder.addAction(bean.icon,bean.text,bean.pendingIntent);
}
}
//headup
if(headup){
cBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
cBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
}else {
cBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
cBuilder.setDefaults(NotificationCompat.DEFAULT_LIGHTS);
}
if(TextUtils.isEmpty(ticker)){
cBuilder.setTicker("你有新的消息");
}
cBuilder.setOngoing(onGoing);
cBuilder.setFullScreenIntent(fullscreenIntent,true);
cBuilder.setVisibility(lockScreenVisiablity);
}
private void previewSettings() {
Notification.Builder builder = new Notification.Builder(this)
.setContentTitle(getString(R.string.lc_preview_notif_title))
.setContentText(String.format(Locale.getDefault(),
getString(R.string.lc_preview_notif_text), getTitle()))
.setSmallIcon(R.drawable.ic_notif_gravitybox)
.setLargeIcon(Icon.createWithResource(this, R.drawable.ic_launcher));
final Notification n = builder.build();
if (mPrefsFragment.getLedMode() == LedMode.OFF) {
n.defaults &= ~Notification.DEFAULT_LIGHTS;
n.flags &= ~Notification.FLAG_SHOW_LIGHTS;
} else if (mPrefsFragment.getLedMode() == LedMode.OVERRIDE) {
n.defaults &= ~Notification.DEFAULT_LIGHTS;
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.ledARGB = mPrefsFragment.getColor();
n.ledOnMS = mPrefsFragment.getLedOnMs();
n.ledOffMS = mPrefsFragment.getLedOffMs();
}
if (mPrefsFragment.getSoundOverride() && mPrefsFragment.getSoundUri() != null) {
n.defaults &= ~Notification.DEFAULT_SOUND;
n.sound = mPrefsFragment.getSoundUri();
}
if (mPrefsFragment.getInsistent()) {
n.flags |= Notification.FLAG_INSISTENT;
}
if (mPrefsFragment.getVibrateOverride()) {
try {
long[] pattern = LedSettings.parseVibratePatternString(
mPrefsFragment.getVibratePatternAsString());
n.defaults &= ~Notification.DEFAULT_VIBRATE;
n.vibrate = pattern;
} catch (Exception e) {
Toast.makeText(this, getString(R.string.lc_vibrate_pattern_invalid),
Toast.LENGTH_SHORT).show();
}
}
if (mPrefsFragment.getVisibility() != Visibility.DEFAULT) {
n.visibility = mPrefsFragment.getVisibility().getValue();
}
if (mPrefsFragment.getVisibilityLs() != VisibilityLs.DEFAULT) {
n.extras.putString(ModLedControl.NOTIF_EXTRA_VISIBILITY_LS,
mPrefsFragment.getVisibilityLs().toString());
}
n.extras.putBoolean("gbIgnoreNotification", true);
Intent intent = new Intent(ModHwKeys.ACTION_SLEEP);
sendBroadcast(intent);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(++NOTIF_ID, n);
}
}, 1000);
}
private void noticeNewBlog(Context context) {
NotificationManager notiManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.cancel(Constants.NOTIFICATION_NEW_MICRO_BLOG);//先清除上一次提醒;
Intent intent = new Intent();
//粉丝
if (entity.getContentType() == Skeleton.TYPE_MORE) {
intent.setAction("com.shejiaomao.weibo.SOCIAL_GRAPH");
intent.addCategory("android.intent.category.DEFAULT");
intent.putExtra("SOCIAL_GRAPH_TYPE", SocialGraphTask.TYPE_FOLLOWERS);
intent.putExtra("USER", account.getUser());
} else {
intent.setAction("com.shejiaomao.weibo.MAIN");
intent.addCategory("android.intent.category.DEFAULT");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
intent.putExtra("CONTENT_TYPE", entity.getContentType());
intent.putExtra("ACCOUNT", account);
Notification notification = new Notification();
notification.icon = R.drawable.icon_notification;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.tickerText = entity.getTickerText();
if (sheJiaoMao.isVibrateNotification()) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
if (sheJiaoMao.isRingtoneNotification()) {
if (StringUtil.isNotEmpty(sheJiaoMao.getRingtoneUri())) {
notification.sound = Uri.parse(sheJiaoMao.getRingtoneUri());
} else {
notification.defaults |= Notification.DEFAULT_SOUND;
}
}
if (sheJiaoMao.isFlashingLEDNotification()) {
notification.ledARGB = Color.GREEN;
notification.ledOffMS = 1000;
notification.ledOnMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
int requestCode = account.getAccountId().intValue() * 100 + entity.getContentType();
PendingIntent pendingIntent = PendingIntent.getActivity(
context, requestCode,
intent, PendingIntent.FLAG_UPDATE_CURRENT
);
notification.setLatestEventInfo(
context, entity.getContentTitle(),
entity.getContentText(), pendingIntent
);
notiManager.notify(requestCode, notification);
}
private static void notify(Context context, int notifyType, int numOfNotifications,
String title, String message) {
// Start building notification
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, message, System.currentTimeMillis());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Link this notification to the Logs activity
Intent notificationIntent = new Intent(context, ActivityLogs.class);
switch (notifyType) {
case NOTIFICATION_ACTION :
notificationIntent.putExtra(ActivityLogs.KEY_TAB_TAG, ActivityLogs.TAB_TAG_ACTION_LOG);
if (numOfNotifications > 1) {
message = context.getString(R.string.notification_action, numOfNotifications);
title = context.getString(R.string.notification_action_title, numOfNotifications);
}
break;
case NOTIFICATION_WARN :
notificationIntent.putExtra(ActivityLogs.KEY_TAB_TAG, ActivityLogs.TAB_TAG_GENERAL_LOG);
if (numOfNotifications > 1) {
message = context.getString(R.string.notification_warn, numOfNotifications);
}
break;
case NOTIFICATION_RULE :
notificationIntent.putExtra(ActivityLogs.KEY_TAB_TAG, ActivityLogs.TAB_TAG_ACTION_LOG);
if (numOfNotifications > 1) {
message = context.getString(R.string.notification_rule, numOfNotifications);
}
break;
default :
Log.w(TAG, new IllegalArgumentException());
return;
}
PendingIntent contentIntent = PendingIntent.getActivity(context, notifyType, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, contentIntent);
// Set Preferences for notification options (sound/vibrate/lights
if (prefs.getBoolean(context.getString(R.string.pref_key_sound), false)) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (prefs.getBoolean(context.getString(R.string.pref_key_vibrate), false)) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
if (prefs.getBoolean(context.getString(R.string.pref_key_light), false)) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
}
// Send the notification
nm.notify(notifyType, notification);
}
/**
* 设置在顶部通知栏中的各种信息
*
* @param pendingIntent
* @param smallIcon
* @param ticker
*/
private void setCompatBuilder(PendingIntent pendingIntent, int smallIcon, String ticker,
String title, String content, boolean sound, boolean vibrate, boolean lights) {
// // 如果当前Activity启动在前台,则不开启新的Activity。
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// // 当设置下面PendingIntent.FLAG_UPDATE_CURRENT这个参数的时候,常常使得点击通知栏没效果,你需要给notification设置一个独一无二的requestCode
// // 将Intent封装进PendingIntent中,点击通知的消息后,就会启动对应的程序
// PendingIntent pIntent = PendingIntent.getActivity(mContext,
// requestCode, intent, FLAG);
cBuilder.setContentIntent(pendingIntent);// 该通知要启动的Intent
cBuilder.setSmallIcon(smallIcon);// 设置顶部状态栏的小图标
cBuilder.setTicker(ticker);// 在顶部状态栏中的提示信息
cBuilder.setContentTitle(title);// 设置通知中心的标题
cBuilder.setContentText(content);// 设置通知中心中的内容
cBuilder.setWhen(System.currentTimeMillis());
/*
* 将AutoCancel设为true后,当你点击通知栏的notification后,它会自动被取消消失,
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
cBuilder.setAutoCancel(true);
// 将Ongoing设为true 那么notification将不能滑动删除
// notifyBuilder.setOngoing(true);
/*
* 从Android4.1开始,可以通过以下方法,设置notification的优先级,
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
cBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
/*
* Notification.DEFAULT_ALL:铃声、闪光、震动均系统默认。
* Notification.DEFAULT_SOUND:系统默认铃声。
* Notification.DEFAULT_VIBRATE:系统默认震动。
* Notification.DEFAULT_LIGHTS:系统默认闪光。
* notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
*/
int defaults = 0;
if (sound) {
defaults |= Notification.DEFAULT_SOUND;
}
if (vibrate) {
defaults |= Notification.DEFAULT_VIBRATE;
}
if (lights) {
defaults |= Notification.DEFAULT_LIGHTS;
}
cBuilder.setDefaults(defaults);
}
/**
* 设置下载进度条Notification显示
*/
private void setupNotificationDownloading(PendingIntent pendingIntent, int smallIcon, String ticker,
String title, String content, boolean sound, boolean vibrate, boolean lights) {
// // 如果当前Activity启动在前台,则不开启新的Activity。
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// // 当设置下面PendingIntent.FLAG_UPDATE_CURRENT这个参数的时候,常常使得点击通知栏没效果,你需要给notification设置一个独一无二的requestCode
// // 将Intent封装进PendingIntent中,点击通知的消息后,就会启动对应的程序
// PendingIntent pIntent = PendingIntent.getActivity(mContext,
// requestCode, intent, FLAG);
notificationBuilder.setContentIntent(pendingIntent);// 该通知要启动的Intent
notificationBuilder.setSmallIcon(smallIcon);// 设置顶部状态栏的小图标
notificationBuilder.setTicker(ticker);// 在顶部状态栏中的提示信息
notificationBuilder.setContentTitle(title);// 设置通知中心的标题
notificationBuilder.setContentText(content);// 设置通知中心中的内容
notificationBuilder.setWhen(System.currentTimeMillis());
/*
* 将AutoCancel设为true后,当你点击通知栏的notification后,它会自动被取消消失,
* 不设置的话点击消息后也不清除,但可以滑动删除
*/
notificationBuilder.setAutoCancel(false);
// 将Ongoing设为true 那么notification将不能滑动删除
// notifyBuilder.setOngoing(true);
/*
* 从Android4.1开始,可以通过以下方法,设置notification的优先级,
* 优先级越高的,通知排的越靠前,优先级低的,不会在手机最顶部的状态栏显示图标
*/
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
/*
* Notification.DEFAULT_ALL:铃声、闪光、震动均系统默认。
* Notification.DEFAULT_SOUND:系统默认铃声。
* Notification.DEFAULT_VIBRATE:系统默认震动。
* Notification.DEFAULT_LIGHTS:系统默认闪光。
* notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
*/
int defaults = 0;
if (sound) {
defaults |= Notification.DEFAULT_SOUND;
}
if (vibrate) {
defaults |= Notification.DEFAULT_VIBRATE;
}
if (lights) {
defaults |= Notification.DEFAULT_LIGHTS;
}
notificationBuilder.setDefaults(defaults);
}