使用Notification在状态栏上显示通知
运行效果图:
结构目录:
注意事项:
如果logcat有错误提示:No Channel found for pkg, 可参考链接:https://blog.csdn.net/u010356768/article/details/83546008
activity_main.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@drawable/background" 8 tools:context=".MainActivity"> 9 10 11 </RelativeLayout>
activity_detail.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@drawable/img" 8 tools:context=".DetailActivity"> 9 10 </RelativeLayout>
MainActivity:
1 package com.mingrisoft.notification; 2 3 import android.app.Notification; 4 import android.app.NotificationChannel; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.content.Intent; 8 import android.graphics.Color; 9 import android.os.Build; 10 import android.support.annotation.RequiresApi; 11 import android.support.v4.app.NotificationCompat; 12 import android.support.v7.app.AppCompatActivity; 13 import android.os.Bundle; 14 import android.util.Log; 15 16 public class MainActivity extends AppCompatActivity { 17 final int NOTIFIYID = 0x123; //通知的ID 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 //获取通知管理器用于发送通知 25 final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 26 //创建一个Notification对象 27 Notification.Builder notification = new Notification.Builder(MainActivity.this); 28 //设置打开该通知,该通知自动消失 29 notification.setAutoCancel(true); 30 //设置通知的图标 31 notification.setSmallIcon(R.drawable.packet); 32 //设置通知的标题 33 notification.setContentTitle("奖励百万红包"); 34 //设置通知的内容 35 notification.setContentText("点击查看详情"); 36 //设置发送时间 37 notification.setWhen(System.currentTimeMillis()); 38 //设置使用系统默认的声音、震动 39 notification.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); 40 //兼容android8.0 41 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 42 NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT); 43 channel.enableLights(true); //是否在桌面icon右上角展示小红点 44 channel.setLightColor(Color.GREEN); //小红点颜色 45 channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知 46 notificationManager.createNotificationChannel(channel); 47 notification.setChannelId("001"); 48 } 49 50 //创建一个启动DetailActivity的Intent 51 Intent intent = new Intent(MainActivity.this, DetailActivity.class); 52 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); 53 //设置通知栏点击跳转 54 notification.setContentIntent(pendingIntent); 55 //发送通知 56 notificationManager.notify(NOTIFIYID, notification.build()); 57 } 58 }
DetailActivity:
1 package com.mingrisoft.notification; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class DetailActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_detail); 12 } 13 }
因为程序要访问系统震动器,所以要声明权限:
manifests:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.mingrisoft.notification"> 4 5 <uses-permission android:name="android.permission.VIBRATE"></uses-permission> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:label="@string/app_name" 11 android:roundIcon="@mipmap/ic_launcher_round" 12 android:supportsRtl="true" 13 android:theme="@style/AppTheme"> 14 <activity android:name=".MainActivity"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 <activity android:name=".DetailActivity"></activity> 22 </application> 23 24 </manifest>