android Notification發(fā)送通知

1、創(chuàng)建 Notification NotificationManager


private NotificationManager manager; 

private  Notification notification;

2、設置通知屬性

public void tz(String strtitlt, String strcontent){
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){   //版本大于等于 安卓8.0
            NotificationChannel channel = new NotificationChannel("leo", "測試通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }

        // 設置取消后的動作
        Intent intent = new Intent();
        intent.setClass(this, OtherActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);


        //初始化 notification
        notification = new NotificationCompat.Builder(this,"leo")
                .setContentTitle(strtitlt)    //設置標題
                .setContentText(strcontent)    //設置通知文字
                .setSmallIcon(R.drawable.ic_launcher_foreground)   //設置左邊的小圖標
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))  //設置大圖標
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)  // 設置點擊通知之后 進入相關頁面(此處進入NotificationActivity類,執(zhí)行oncreat方法打印日志)
                .setAutoCancel(true)   //設置點擊通知后 通知通知欄不顯示
                .build();
}

3、設置發(fā)送標題與內容

String title = "通知標題";
String content = "通知內容";

4、加載設置與發(fā)送通知

tz(title, content);
manager.notify(1,notification);

5、完整MainActivity內容

public class MainActivity extends AppCompatActivity {

    private NotificationManager manager;
    private  Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String title = "通知標題";
        String content = "通知內容";
        tz(title, content);
        manager.notify(1,notification);
    }

    //開啟通知按鈕  點擊事件
    public void setNotification(View view) {
        manager.notify(1,notification);    //點擊事件啟動喚醒manager,用于通知notification
    }

    //關閉通知按鈕  點擊事件
    public void cacelNotification(View view) {
        manager.cancel(1);   //取消通知
    }

    public void tz(String strtitlt, String strcontent){
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){   //版本大于等于 安卓8.0
            NotificationChannel channel = new NotificationChannel("leo", "測試通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }

        // 設置取消后的動作
        Intent intent = new Intent();
        intent.setClass(this, OtherActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);


        //初始化 notification
        notification = new NotificationCompat.Builder(this,"leo")
                .setContentTitle(strtitlt)    //設置標題
                .setContentText(strcontent)    //設置通知文字
                .setSmallIcon(R.drawable.ic_launcher_foreground)   //設置左邊的小圖標
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))  //設置大圖標
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)  // 設置點擊通知之后 進入相關頁面(此處進入OtherActivity類)
                .setAutoCancel(true)   //設置點擊通知后 通知通知欄不顯示
                .build();
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容