日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中使用Notification在通知栏中显示通知

發布時間:2025/3/19 Android 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中使用Notification在通知栏中显示通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

App在接收到后臺推送的消息后,需要在系統通知欄中顯示通知消息,并且點擊通知消息跳轉到新的頁面,并將消息內容傳遞過去。

效果如下

?

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

首先在主頁面添加一個按鈕

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/showNotice"android:text="顯示通知"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

然后在activity中設置按鈕的點擊事件

??? private Button showNoticeButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showNoticeButton = findViewById(R.id.showNotice);showNoticeButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {showNotification(MainActivity.this, "通知內容:關注公眾號,編程資料與技術共享學習");}});}

在點擊事件中調用了方法showNotification并且傳遞了消息的內容,在方法showNotification中

??? public void showNotification(Context context, String content) {//1.創建通知管理器NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Android 8.0版本適配NotificationChannel channel = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_HIGH);notificationManager.createNotificationChannel(channel);builder = new NotificationCompat.Builder(context, "default");} else {builder = new NotificationCompat.Builder(context);}//用來進行跳轉并傳遞消息的intentIntent intent = new Intent(this, NoticeActivity.class);intent.putExtra("content",content);//2.創建通知實例Notification notification = builder.setContentTitle("通知標題:公眾號-霸道的程序猿").setContentText(content).setWhen(System.currentTimeMillis())//smallIcon 通知欄顯示小圖標//android5.0 之后通知欄圖標都修改了,小圖標不能含有RGB圖層,也就是說圖片不能帶顏色,否則顯示的就成白色方格了//解決方法一:為圖片帶顏色,targetSdkVersion改為21以下//解決方法二:只能用白色透明底的圖片.setSmallIcon(R.mipmap.ic_launcher)//LargeIcon 下拉后顯示的圖標.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//收到通知時的效果,這里是默認聲音.setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true).setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)).build();//3.notify//notifyId每次要不一致,不然下一次的通知會覆蓋上一次int notifyId = new Random().nextInt();notificationManager.notify(notifyId, notification);}

首先創建通知管理器,然后創建通知實例,在創建通知實例時傳遞了一個Intent對象用來進行點擊通知消息的跳轉,通過

??????? //用來進行跳轉并傳遞消息的intentIntent intent = new Intent(this, NoticeActivity.class);intent.putExtra("content",content);

傳遞要顯示的消息內容

然后新建一個Activity叫NoticeActivity用來實現點擊通知后跳轉顯示消息內容。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".NoticeActivity"><TextViewandroid:id="@+id/content_TextView"android:layout_width="match_parent"android:layout_height="wrap_content"/></RelativeLayout>

添加一個TextView用來顯示消息內容

然后在Activity中

??? @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notice);String content = getIntent().getStringExtra("content");TextView textView = findViewById(R.id.content_TextView);textView.setText("消息內容:\n"+content);}

獲取傳遞的參數并顯示消息內容。

效果

點擊顯示通知按鈕

?

點擊通知內容

?

?

?

總結

以上是生活随笔為你收集整理的Android中使用Notification在通知栏中显示通知的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。