版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/soha_dong/article/details/80077087
写一个工具类来显示安卓的通知栏
public static void showNotification(Context context, String title, String msg, Class<?> activity) {
PendingIntent pendingIntent = null;
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
if (activity != null) {
Intent intent = new Intent(context, activity);
intent.putExtra("i", 1);
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
notification = new Notification.Builder(context)
.setContentIntent(pendingIntent)//跳转的activity
.setContentTitle(title).setAutoCancel(true)//标题和点击消失
.setContentText(msg)//文本
.setSmallIcon(R.mipmap.ic_launcher)//图标
.build();
} else {
notification = new Notification.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle(title).setAutoCancel(true)
.setContentText(msg)
.setSmallIcon(R.mipmap.ic_launcher)
.getNotification();
}
manager.notify(123, notification);
}
}
调用方式:
MyUtils.showNotification(getApplicationContext(),"title","message",MainActivity.class);