记录一下Notification的使用方法,方便日后查看。
----------------------------------------------------------------------------------------------------------------------------------------------------
在Android 8及以后,Android使用以下方式来实现Notification
NotificationCompat.Builder(Context context, String channelId)
总结一下,创建一个Notification需要三步:
1、创建一个 NotificationChannel
NotificationChannel(String id, CharSequence name, int importance)
2、创建一个 NotificationCompat.Builder
NotificationCompat.Builder(Context context, String channelId)
3、创建一个 NotificationManager 进行管理
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
一、一个简单实现的Notification如下:
package com.example.chy.notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
/**
* ************************************
* 项目名称:Notification
*
* 创建时间: 2018/12/13 15:54
* 用途
* ************************************
*/
public class NotificationNormal {
public static String TAG = "----NotificationNormal----";
public static void setNotification(Context context) {
String str = "chy-" + System.currentTimeMillis();
//NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(str,"chy",NotificationManager.IMPORTANCE_LOW);
//NotificationCompat.Builder
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context,str);
mbuilder.setContentText("测试内容 测试内容 测试内容 测试内容 测试内容 测试内容 测试内容") //设置通知栏显示内容
.setContentTitle("测试标题")
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setSmallIcon(R.drawable.status_music);//设置通知小ICON
//NotificationManager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,mbuilder.build());
}
}
-------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------
二、还可以实现自定义的 Notification
package com.example.chy.notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
/**
* ************************************
* 项目名称:Notification
*
* 创建时间: 2018/12/13 16:02
* 用途
* ************************************
*/
public class NotificationCustomize {
public static String TAG = "----NotificationCustomize----";
public static void setNotification(Context context) {
String str = "chy-" + System.currentTimeMillis();
//NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(str,"chy",NotificationManager.IMPORTANCE_LOW);
//NotificationCompat.Builder
final RemoteViews collapsed = new RemoteViews(context.getPackageName(),R.layout.statusbar);
NotificationCompat.Builder status = new NotificationCompat.Builder(context,str);
status.setCustomContentView(collapsed)
.setContentTitle("测试标题")
.setContentText("测试内容")
.setSmallIcon(R.drawable.status_music);//设置通知小ICON;
//NotificationManager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,status.build());
}
}
自定义notification涉及的布局文件在R.layout.statusbar中定义。
-----------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------
三、给自定义的notification设置点击事件
package com.example.chy.notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
/**
* ************************************
* 项目名称:Notification
* 创建时间: 2018/12/14 12:18
* 用途
* ************************************
*/
public class NotificationPendingIntent {
public String TAG = "----NotificationPendingIntent----";
public Context mcontext ;
public final String TOGGLEPAUSE_ACTION = "com.android.music.togglepause";
public final String PREVIOUS_ACTION = "com.android.music.previous";
public final String NEXT_ACTION = "com.android.music.next";
public void setNotification(Context context) {
String str = "chy-" + System.currentTimeMillis();
mcontext = context;
//NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(str,"chy",NotificationManager.IMPORTANCE_LOW);
//NotificationCompat.Builder
final RemoteViews collapsed = new RemoteViews(context.getPackageName(),R.layout.statuspendingintent);
//自定义notification
setPendingIntent(collapsed);
NotificationCompat.Builder status = new NotificationCompat.Builder(context,str);
status.setCustomContentView(collapsed)
.setSmallIcon(R.drawable.status_music);//设置通知小ICON;
//NotificationManager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,status.build());
}
private void setPendingIntent(RemoteViews collapsed) {
//注册广播
broadcastRegister();
PendingIntent prePendingIntent = PendingIntent.getBroadcast(mcontext, 0, new Intent(
PREVIOUS_ACTION), 0);
PendingIntent togglePendingIntent = PendingIntent.getBroadcast(mcontext, 0, new Intent(
TOGGLEPAUSE_ACTION), 0);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(mcontext, 0, new Intent(
NEXT_ACTION), 0);
collapsed.setOnClickPendingIntent(R.id.pre_btn,prePendingIntent);
collapsed.setOnClickPendingIntent(R.id.toggle_btn,togglePendingIntent);
collapsed.setOnClickPendingIntent(R.id.next_btn,nextPendingIntent);
}
private void broadcastRegister() {
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(TOGGLEPAUSE_ACTION);
commandFilter.addAction(NEXT_ACTION);
commandFilter.addAction(PREVIOUS_ACTION);
mcontext.registerReceiver(mIntentReceiver, commandFilter);
}
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case PREVIOUS_ACTION :
Log.d(TAG,"pre song");
Toast.makeText(mcontext,"pre song",Toast.LENGTH_SHORT).show();
break;
case TOGGLEPAUSE_ACTION:
Log.d(TAG,"toggle pause");
Toast.makeText(mcontext,"toggle pause",Toast.LENGTH_SHORT).show();
break;
case NEXT_ACTION:
Log.d(TAG,"next song");
Toast.makeText(mcontext,"next song",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
}
以后有新的理解再更新。