android 通知使用方法(適配android8.0前后)

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);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末侣肄,一起剝皮案震驚了整個濱河市肠牲,隨后出現(xiàn)的幾起案子玲销,更是在濱河造成了極大的恐慌镐捧,老刑警劉巖遭赂,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件循诉,死亡現(xiàn)場離奇詭異,居然都是意外死亡撇他,警方通過查閱死者的電腦和手機茄猫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來困肩,“玉大人划纽,你說我怎么就攤上這事⌒炕” “怎么了勇劣?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長潭枣。 經(jīng)常有香客問我比默,道長,這世上最難降的妖魔是什么卸耘? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任退敦,我火速辦了婚禮,結(jié)果婚禮上蚣抗,老公的妹妹穿的比我還像新娘侈百。我一直安慰自己,他們只是感情好翰铡,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布钝域。 她就那樣靜靜地躺著,像睡著了一般锭魔。 火紅的嫁衣襯著肌膚如雪例证。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天迷捧,我揣著相機與錄音织咧,去河邊找鬼胀葱。 笑死,一個胖子當著我的面吹牛笙蒙,可吹牛的內(nèi)容都是我干的抵屿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捅位,長吁一口氣:“原來是場噩夢啊……” “哼轧葛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起艇搀,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤尿扯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后焰雕,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體衷笋,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年淀散,在試婚紗的時候發(fā)現(xiàn)自己被綠了右莱。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蚜锨。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡档插,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出亚再,到底是詐尸還是另有隱情郭膛,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布氛悬,位于F島的核電站则剃,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏如捅。R本人自食惡果不足惜棍现,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望镜遣。 院中可真熱鬧己肮,春花似錦、人聲如沸悲关。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽寓辱。三九已至艘绍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秫筏,已是汗流浹背诱鞠。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工挎挖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人航夺。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓肋乍,卻偏偏與公主長得像,于是被迫代替她去往敵國和親敷存。 傳聞我的和親對象是個殘疾皇子墓造,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容