import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
static final int NOTIFICATION_ID = 0x123;
NotificationManager nm;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn_1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //判斷版本號
String channelId = "chat";
String channelName = "聊天消息";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
channelId = "subscribe";
channelName = "訂閱消息";
importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this, "chat")
.setContentTitle("收到一條聊天消息")
.setContentText("今天中午吃什么?")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setAutoCancel(true)
.build();
manager.notify(1, notification);
}
});
}else {
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
Notification_Four();
}
});
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
} //設(shè)置渠道
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void Notification_Four(){
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
Notification notifi = new Notification.Builder(MainActivity.this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setTicker("有新消息")
.setContentTitle("一條新通知")
.setContentText("hello word")
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.build();
nm.notify(NOTIFICATION_ID,notifi);
} //安卓8.0以下系統(tǒng)適用
public void sendSubscribeMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "subscribe")
.setContentTitle("收到一條訂閱消息")
.setContentText("地鐵沿線30萬商鋪搶購中锯茄!")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setAutoCancel(true)
.build();
manager.notify(2, notification);
}
}
android 通知使用方法(適配android8.0前后)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來困肩,“玉大人划纽,你說我怎么就攤上這事⌒炕” “怎么了勇劣?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長潭枣。 經(jīng)常有香客問我比默,道長,這世上最難降的妖魔是什么卸耘? 我笑而不...
- 正文 為了忘掉前任退敦,我火速辦了婚禮,結(jié)果婚禮上蚣抗,老公的妹妹穿的比我還像新娘侈百。我一直安慰自己,他們只是感情好翰铡,可當我...
- 文/花漫 我一把揭開白布钝域。 她就那樣靜靜地躺著,像睡著了一般锭魔。 火紅的嫁衣襯著肌膚如雪例证。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼捅位,長吁一口氣:“原來是場噩夢啊……” “哼轧葛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起艇搀,我...
- 正文 年R本政府宣布氛悬,位于F島的核電站则剃,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏如捅。R本人自食惡果不足惜棍现,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望镜遣。 院中可真熱鬧己肮,春花似錦、人聲如沸悲关。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽寓辱。三九已至艘绍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秫筏,已是汗流浹背诱鞠。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 摘自郭神的文章: 從Android 8.0系統(tǒng)開始,Google引入了通知渠道這個概念锚烦。 什么是通知渠道呢觅闽?顧名思...
- 1創(chuàng)建Service package com.shiliu.callrecording; import andro...
- 使用Firebase云消息傳遞FCM和PHP的Android推送通知 最近,谷歌從谷歌云消息傳遞(GCM)遷移到F...