package com.temp.flexboxdemo;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.widget.TextView;
public class NotificationUtil extends ContextWrapper {
private NotificationManager mManager;
public static final String sID = "channel_1";
public static final String sName = "channel_name_1";
public NotificationUtil(Context base) {
super(base);
}
private NotificationManager getmManager(){
if(mManager == null){
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
return mManager;
}
public void sendNotification(String title, String content) {
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= 26) {
createNotificationChannel();
Notification notification = getNotification_26(title, content).setContentIntent(pendingIntent).build();
getmManager().notify(1, notification);
} else {
Notification notification = getNotification_25(title, content).setContentIntent(pendingIntent).build();
getmManager().notify(1, notification);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void createNotificationChannel(){
NotificationChannel notificationChannel = new NotificationChannel(sID,sName,NotificationManager.IMPORTANCE_HIGH);
getmManager().createNotificationChannel(notificationChannel);
}
public NotificationCompat.Builder getNotification_25(String title, String content) {
// 以下是展示大图的通知
android.support.v4.app.NotificationCompat.BigPictureStyle style = new android.support.v4.app.NotificationCompat.BigPictureStyle();
style.setBigContentTitle("BigContentTitle");
style.setSummaryText("SummaryText");
style.bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));
// 以下是展示多文本通知
android.support.v4.app.NotificationCompat.BigTextStyle style1 = new android.support.v4.app.NotificationCompat.BigTextStyle();
style1.setBigContentTitle(title);
style1.bigText(content);
return new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(title)
.setContentText(content)
.setTicker("setTicker")
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
.setStyle(style)
.setAutoCancel(true);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder getNotification_26(String title, String content) {
return new Notification.Builder(getApplicationContext(), sID)
.setContentTitle(title)
.setContentText(content)
.setTicker("setTicker")
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
.setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background)))
.setNumber(1)
.setAutoCancel(true);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void notifyDownloading(long progress, long num, String file_name) {
Notification.Builder mBuilder;
mBuilder = new Notification.Builder(getApplicationContext(), sID);
NotificationChannel channel;
channel = new NotificationChannel(sID , file_name, NotificationManager.IMPORTANCE_HIGH);
getmManager().createNotificationChannel(channel);
mBuilder.setSmallIcon(R.drawable.ic_launcher_background);
mBuilder.setProgress((int) num, (int) progress, false);
mBuilder.setContentInfo("1231231");
mBuilder.setOngoing(true);
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setContentTitle(file_name);
mBuilder.setContentText("download");
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendIntent);
getmManager().notify(1, mBuilder.build());
}
}
Notification 的基本使用
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.通知的使用场合 当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。...
- Notification状态栏通知 1建造者模式构建通知类:Notification.Builder 2通知管理器...
- 使用Notifaction和NotifactionManager的目的 Broadcast Receiver组件并...