下面列出了android.app.Notification#setLatestEventInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message, String type, int activity_num) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
if (type.equals("ODM"))
notificationIntent.setData(Uri.parse("https://github.com/Fmstrat/odm/raw/master/latest/odm.apk"));
else
notificationIntent.setData(Uri.parse("https://github.com/Fmstrat/odm-web"));
PendingIntent intent = PendingIntent.getActivity(context, activity_num, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(activity_num, notification);
}
private void showNotification(long cancelTime, String text) {
try {
Context app = getContext().getApplicationContext();
NotificationManager nm = (NotificationManager) app
.getSystemService(Context.NOTIFICATION_SERVICE);
final int id = Integer.MAX_VALUE / 13 + 1;
nm.cancel(id);
long when = System.currentTimeMillis();
Notification notification = new Notification(notifyIcon, text, when);
PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(), 0);
notification.setLatestEventInfo(app, notifyTitle, text, pi);
notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(id, notification);
if (cancelTime > 0) {
Message msg = new Message();
msg.what = MSG_CANCEL_NOTIFY;
msg.obj = nm;
msg.arg1 = id;
UIHandler.sendMessageDelayed(msg, cancelTime, this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void showNotification(long cancelTime, String text) {
try {
Context app = getApplicationContext();
NotificationManager nm = (NotificationManager) app
.getSystemService(Context.NOTIFICATION_SERVICE);
final int id = Integer.MAX_VALUE / 13 + 1;
nm.cancel(id);
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.ic_launcher, text, when);
PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(), 0);
notification.setLatestEventInfo(app, "sharesdk test", text, pi);
notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(id, notification);
if (cancelTime > 0) {
Message msg = new Message();
msg.what = MSG_CANCEL_NOTIFY;
msg.obj = nm;
msg.arg1 = id;
UIHandler.sendMessageDelayed(msg, cancelTime, this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void showNotification(long cancelTime, String text) {
try {
Context app = getContext().getApplicationContext();
NotificationManager nm = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
final int id = Integer.MAX_VALUE / 13 + 1;
nm.cancel(id);
long when = System.currentTimeMillis();
Notification notification = new Notification(notifyIcon, text, when);
PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(), 0);
notification.setLatestEventInfo(app, notifyTitle, text, pi);
notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(id, notification);
if (cancelTime > 0) {
Message msg = new Message();
msg.what = MSG_CANCEL_NOTIFY;
msg.obj = nm;
msg.arg1 = id;
UIHandler.sendMessageDelayed(msg, cancelTime, this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Show a notification while this service is running.
*/
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.remote_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.stat_sample, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Controller.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.remote_service_label),
text, contentIntent);
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
mNM.notify(R.string.remote_service_started, notification);
}
public void onReceive(Context context, Intent intent) {
SharedPreferences shareDate =
context.getSharedPreferences("timing",Activity.MODE_PRIVATE);
int date = shareDate.getInt("date",1);
String str = getNowDate(context, date);
boolean flag = matchRecode(context, str);
Log.v("test_dick",flag+"");
if(flag){
//context.startService(new Intent(context, cn.scau.scautreasure.service.NotificationService.class));
try {
NotificationManager mNotificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationIcon= R.drawable.icon;
CharSequence notificationTitle="来自华农宝";
long when = System.currentTimeMillis();
Notification notification=new Notification(notificationIcon, notificationTitle, when);
notification.defaults=Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intentservice=new Intent(context.getApplicationContext(), BorrowedBook_.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context.getApplicationContext(), 0, intentservice, 0);
notification.setLatestEventInfo(context.getApplicationContext(),"借阅到期提示", "你有N本书即将满借阅",pendingIntent);
mNotificationManager.notify(1000, notification);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private int showSendingNotification() {
final int id = 10;
final Notification notification = new Notification(
R.drawable.ic_notify_icon, "饭否私信正在发送...",
System.currentTimeMillis());
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(), 0);
notification.setLatestEventInfo(this, "饭否私信", "正在发送...", contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
this.nm.notify(id, notification);
return id;
}
public static Notification add(Notification notification, Context context,
CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent,
PendingIntent fullScreenIntent) {
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.fullScreenIntent = fullScreenIntent;
return notification;
}
public Notification build(Builder b) {
Notification result = (Notification) b.mNotification;
result.setLatestEventInfo(b.mContext, b.mContentTitle,
b.mContentText, b.mContentIntent);
// translate high priority requests into legacy flag
if (b.mPriority > PRIORITY_DEFAULT) {
result.flags |= FLAG_HIGH_PRIORITY;
}
return result;
}
private void sendNotifycation(int notifyId, int textId, int drawableId) {
NotificationManager notificationManager = (NotificationManager) getSystemService("notification");
Notification notification = new Notification();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
notification.icon = drawableId;
notification.tickerText = StringUtils.getString(textId);
notification.defaults |= 1;
notification.flags = 16;
notification.setLatestEventInfo(this, null, null, contentIntent);
notificationManager.notify(notifyId, notification);
notificationManager.cancel(notifyId);
if (LetvUtils.getBrandName().toLowerCase().contains("xiaomi")) {
ToastUtils.showToast((Context) this, textId);
}
}
/**
* show current enviroment
*
* @param rscId
*/
private void showNotification(int rscId) {
Notification n = new Notification(R.drawable.switchenv, getResources().getString(rscId),
(System.currentTimeMillis() + 1000));
n.flags = Notification.FLAG_NO_CLEAR;
n.setLatestEventInfo(context, getResources().getString(R.string.app_name), getResources().getString(rscId),
null);
n.contentView.setViewVisibility(android.R.id.progress, View.GONE);
n.contentIntent = PendingIntent.getActivity(context, 0, new Intent(this, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
notiManager.notify(0, n);
}
private void startForeground(Class<? extends Activity> clazz, Intent intent) {
int icon = intent.getIntExtra(KEY_NOTIFACION_ICON, 17301540);
String contentTitle = intent.getStringExtra(KEY_NOTIFACION_CONTENTTITLE);
String contentText = intent.getStringExtra(KEY_NOTIFACION_CONTENTTEXT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, clazz), 0);
Notification notification = new Notification(icon, contentText, System.currentTimeMillis());
notification.flags = 2;
notification.flags |= 32;
notification.flags |= 64;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
startForeground(0, notification);
}
public static Notification getNotification(Context context, PendingIntent pendingIntent, Track track, boolean isPlaying) {
Notification notification = new Notification();
if (track != null) {
notification.contentView = getNotificationViews(track, context, isPlaying);
}
else {
notification.setLatestEventInfo(context, "", "", pendingIntent);
}
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
notification.contentIntent = pendingIntent;
notification.icon = R.drawable.ic_notification;
return notification;
}
/**
* 通知栏提醒
* @param messageItem 消息信息
*/
@SuppressWarnings("deprecation")
private void showMessage(ChatMessageItem messageItem) {
SettingSPUtil setSP = MyApp.getInstance().getSettingSPUtil();
if (!setSP.isChatNotiOnBar()){
return;
}
String messageContent = messageItem.getNickName()+":"+messageItem.getMessage();
MyApp.getInstance().setNewMsgCount(MyApp.getInstance().getNewMsgCount()+1); //数目+1
Notification notification = new Notification(R.drawable.ic_launcher,messageContent, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
if (setSP.isChatRing()){
// 设置默认声音
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (setSP.isChatVibrate()){
// 设定震动(需加VIBRATE权限)
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
Intent intent = new Intent(MyApp.getInstance(),ChatRoomAty.class);
intent.putExtra("TrainId", messageItem.getTrainId());
PendingIntent contentIntent = PendingIntent.getActivity(MyApp.getInstance(), 0, intent, 0);
String contentText = null;
if(MyApp.getInstance().getNewMsgCount()==1){
contentText = messageContent;
}else{
contentText = MyApp.getInstance().getNewMsgCount()+"条未读消息";
}
notification.setLatestEventInfo(MyApp.getInstance(), "车友聊天室", contentText, contentIntent);
NotificationManager manage = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
manage.notify(NOTIFICATION_ID, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = new Notification(R.drawable.ic_launcher, "BLE Logger",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, BluetoothLeService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "BLE","BLE Logger", pendingIntent);
this.startForeground(1, notification);
return Service.START_NOT_STICKY;
}
/**
* Return a persistent {@link Notification} for the corresponding id. You
* must return a notification for AT LEAST the first id to be requested.
* Once the persistent notification is shown, further calls to
* {@link #getPersistentNotification(int)} may return null. This way Android
* can start the StandOut window service in the foreground and will not kill
* the service on low memory.
*
* <p>
* As a courtesy, the system will request a notification for every new id
* shown. Your implementation is encouraged to include the
* {@link PendingIntent#FLAG_UPDATE_CURRENT} flag in the notification so
* that there is only one system-wide persistent notification.
*
* <p>
* See the StandOutExample project for an implementation of
* {@link #getPersistentNotification(int)} that keeps one system-wide
* persistent notification that creates a new window on every click.
*
* @param id
* The id of the window.
* @return The {@link Notification} corresponding to the id, or null if
* you've previously returned a notification.
*/
public Notification getPersistentNotification(int id) {
// basic notification stuff
// http://developer.android.com/guide/topics/ui/notifiers/notifications.html
int icon = getAppIcon();
long when = System.currentTimeMillis();
Context c = getApplicationContext();
String contentTitle = getPersistentNotificationTitle(id);
String contentText = getPersistentNotificationMessage(id);
String tickerText = String.format("%s: %s", contentTitle, contentText);
// getPersistentNotification() is called for every new window
// so we replace the old notification with a new one that has
// a bigger id
Intent notificationIntent = getPersistentNotificationIntent(id);
PendingIntent contentIntent = null;
if (notificationIntent != null) {
contentIntent = PendingIntent.getService(this, 0, notificationIntent,
// flag updates existing persistent notification
PendingIntent.FLAG_UPDATE_CURRENT);
}
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(c, contentTitle, contentText, contentIntent);
return notification;
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
super.onStart(intent, startId);
try {
NotificationManager mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int notificationIcon= R.drawable.icon;
CharSequence notificationTitle="来自华农宝";
long when = System.currentTimeMillis();
Notification notification=new Notification(notificationIcon, notificationTitle, when);
notification.defaults=Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intentservice=new Intent(getApplicationContext(), BorrowedBook_.class);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 0, intentservice, 0);
notification.setLatestEventInfo(getApplicationContext(),"借阅到期提示", "你有N本书即将满借阅",pendingIntent);
mNotificationManager.notify(1000, notification);
} catch (Exception e) {
e.printStackTrace();
}
return Service.START_CONTINUATION_MASK;
}
public Notification build(Builder b) {
Notification result = (Notification) b.mNotification;
result.setLatestEventInfo(b.mContext, b.mContentTitle,
b.mContentText, b.mContentIntent);
result = NotificationCompatGingerbread.add(result, b.mContext,
b.mContentTitle, b.mContentText, b.mContentIntent, b.mFullScreenIntent);
// translate high priority requests into legacy flag
if (b.mPriority > PRIORITY_DEFAULT) {
result.flags |= FLAG_HIGH_PRIORITY;
}
return result;
}
@SuppressWarnings("deprecation")
public void prepareNotification(final int notifId, UpdateInfoModel chapter, Notification notification) {
CharSequence contentTitle = chapter.getUpdateType().toString();
CharSequence contentText = chapter.getUpdateTitle();
Intent notificationIntent = new Intent(this, MainTabActivity.class);
notificationIntent.putExtra(Constants.EXTRA_PAGE, chapter.getUpdatePage());
int pendingFlag = PendingIntent.FLAG_CANCEL_CURRENT;
PendingIntent contentIntent = PendingIntent.getActivity(this, notifId, notificationIntent, pendingFlag);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
}
/**
* The notification is the icon and associated expanded entry in the
* status bar.
*/
protected void showNotification() {
// look up the notification manager service
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// The details of our fake message
CharSequence from = "Joe";
CharSequence message = "kthx. meet u for dinner. cul8r";
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, IncomingMessageView.class), 0);
// The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message);
// construct the Notification object.
Notification notif = new Notification(R.drawable.stat_sample, tickerText,
System.currentTimeMillis());
// Set the info for the views that show in the notification panel.
notif.setLatestEventInfo(this, from, message, contentIntent);
/*
// On tablets, the ticker shows the sender, the first line of the message,
// the photo of the person and the app icon. For our sample, we just show
// the same icon twice. If there is no sender, just pass an array of 1 Bitmap.
notif.tickerTitle = from;
notif.tickerSubtitle = message;
notif.tickerIcons = new Bitmap[2];
notif.tickerIcons[0] = getIconBitmap();;
notif.tickerIcons[1] = getIconBitmap();;
*/
// after a 0ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms.
notif.vibrate = new long[] { 0, 250, 100, 500};
// Note that we use R.layout.incoming_message_panel as the ID for
// the notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your
// application.
nm.notify(R.string.imcoming_message_ticker_text, notif);
}