之前項目一直使用極光推送,極光推送自帶通知提示,由於項目需求,
極光的推送提示不能滿足要求,所以只能自己完成推送提示操作。
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import com.leiren.airport.plat.R;
import com.leiren.airport.plat.activity.HomeActivity;
import java.util.Date;
public class CustomNotification {
//自定義通知對象
private static CustomNotification customNotification = null;
//通知管理器
private static NotificationManager notificationManager = null;
//通知ID
private static int notifiId = 0;
//通知標籤
private static String notifiTag = "";
static {
customNotification = new CustomNotification();
}
private CustomNotification(){
super();
}
public static CustomNotification getInstance(){
return customNotification == null? new CustomNotification():customNotification;
}
public void builderNotification(Context context,String sender,String title, String content, String extra){
//此處判斷安卓版本號是否大於或者等於Android8.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//通知欄提示
notification_8(context,sender,title,content,extra);
} else {
//通知欄提示
commonNotification(context,sender,title,content,extra);
}
}
/**
* Android5.0-8.0以下
* @param context 上下文對象
* @param sender 一般爲app名稱
* @param title 推送標題
* @param content 推送內容
*/
@TargetApi(21)
private void commonNotification(Context context,String sender,String title, String content, String extra){
//獲取通知管理器,用於發送通知
notificationManager = notificationManager == null?
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE):notificationManager;
//發送通知
sendNotification(context,sender,title,content,extra);
}
/**
* android8.0以上
* @param context
* @param sender
* @param title
* @param content
* @param extra
*/
@TargetApi(Build.VERSION_CODES.O)
public void notification_8(Context context,String sender,String title, String content, String extra){
//創建通道對象
NotificationChannel channel = new NotificationChannel("com.leiren.airplat", "其他", NotificationManager.IMPORTANCE_HIGH);
notificationManager = notificationManager == null?
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE):notificationManager;
//設置通道
notificationManager.createNotificationChannel(channel);
//發送通知
sendNotification(context,sender,title,content,extra);
}
private void sendNotification
(Context context, String sender, String title,String content,String extra){
// 創建一個Notification對象
final NotificationCompat.Builder notification =
new NotificationCompat.Builder(context);
// 設置打開該通知,該通知自動消失
notification.setAutoCancel(true);
// 設置顯示在狀態欄的通知提示信息
notification.setTicker(sender);
// 設置通知的小圖標
notification.setSmallIcon(R.drawable.ic_launcher);
//設置下拉列表中的大圖標
notification.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher));
// 設置通知內容的標題
notification.setContentTitle(title);
// 設置通知內容
notification.setContentText(content);
//設置發送時間
notification.setWhen(System.currentTimeMillis());
notification.setPriority(Notification.PRIORITY_MAX);//設置通知的優先級
//向通知添加聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,可以組合
notification.setDefaults(Notification.DEFAULT_ALL);
//設置通知的等級
// VISIBILITY_PUBLIC: 任何情況的顯示
//VISIBILITY_PRIVATE: 只有在沒有鎖屏時顯示
//VISIBILITY_SECRET: 在安全鎖下或者沒鎖屏下顯示
notification.setVisibility(Notification.VISIBILITY_PUBLIC);
// 關聯PendingIntent 設置之後會一直顯示懸浮通知不會消失
//notification.setFullScreenIntent(pIntent1, true);
// 創建一個啓動其他Activity的Intent
Intent intent = new Intent("cn.jpush.android.intent.NOTIFICATION_OPENED");
intent.putExtra("cn.jpush.android.EXTRA",extra);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
Intent intent1 = new Intent(context, HomeActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//設置通知欄點擊跳轉
notification.setContentIntent(pIntent);
//發送通知
notifiId = (int) new Date().getTime();//通知Id
notifiTag = notifiId+"leiren";//通知標籤
notificationManager.notify(notifiTag,notifiId, notification.build());
}
}
遇見的問題:
在寫完之後發現android9.0一直沒有橫幅提示,而在android7.0上邊橫幅提示一直不能自動消失,百度了很多資料,
實現橫幅提示有兩種方法:
1、設置通知的優先級
2、setFullScreenIntent方法
我把兩個都設置了,結果出現了上述問題,最後我把setFullScreenIntent方法註釋了,android9.0的橫幅提示就出現了,
同時android7.0的橫幅提示也會自動消失。
Android橫幅通知欄自定義
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.