android Notification發(fā)送通知

1借尿、創(chuàng)建 Notification NotificationManager


private NotificationManager manager; 

private  Notification notification;

2民鼓、設(shè)置通知屬性

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);
        }

        // 設(shè)置取消后的動作
        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)    //設(shè)置標(biāo)題
                .setContentText(strcontent)    //設(shè)置通知文字
                .setSmallIcon(R.drawable.ic_launcher_foreground)   //設(shè)置左邊的小圖標(biāo)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))  //設(shè)置大圖標(biāo)
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)  // 設(shè)置點擊通知之后 進入相關(guān)頁面(此處進入NotificationActivity類,執(zhí)行oncreat方法打印日志)
                .setAutoCancel(true)   //設(shè)置點擊通知后 通知通知欄不顯示
                .build();
}

3霹琼、設(shè)置發(fā)送標(biāo)題與內(nèi)容

String title = "通知標(biāo)題";
String content = "通知內(nèi)容";

4务傲、加載設(shè)置與發(fā)送通知

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

5、完整MainActivity內(nèi)容

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 = "通知標(biāo)題";
        String content = "通知內(nèi)容";
        tz(title, content);
        manager.notify(1,notification);
    }

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

    //關(guān)閉通知按鈕  點擊事件
    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);
        }

        // 設(shè)置取消后的動作
        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)    //設(shè)置標(biāo)題
                .setContentText(strcontent)    //設(shè)置通知文字
                .setSmallIcon(R.drawable.ic_launcher_foreground)   //設(shè)置左邊的小圖標(biāo)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))  //設(shè)置大圖標(biāo)
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)  // 設(shè)置點擊通知之后 進入相關(guān)頁面(此處進入OtherActivity類)
                .setAutoCancel(true)   //設(shè)置點擊通知后 通知通知欄不顯示
                .build();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末售葡,一起剝皮案震驚了整個濱河市缚态,隨后出現(xiàn)的幾起案子误证,更是在濱河造成了極大的恐慌刺覆,老刑警劉巖柑爸,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異琳要,居然都是意外死亡恢共,警方通過查閱死者的電腦和手機晃听,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門榨咐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來介却,“玉大人,你說我怎么就攤上這事块茁〕菘溃” “怎么了桂肌?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長永淌。 經(jīng)常有香客問我崎场,道長,這世上最難降的妖魔是什么遂蛀? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任照雁,我火速辦了婚禮,結(jié)果婚禮上答恶,老公的妹妹穿的比我還像新娘。我一直安慰自己萍诱,他們只是感情好悬嗓,可當(dāng)我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著裕坊,像睡著了一般包竹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上籍凝,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天周瞎,我揣著相機與錄音,去河邊找鬼饵蒂。 笑死声诸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的退盯。 我是一名探鬼主播彼乌,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼渊迁!你這毒婦竟也來了慰照?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤琉朽,失蹤者是張志新(化名)和其女友劉穎毒租,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體箱叁,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡墅垮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蝌蹂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片噩斟。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖孤个,靈堂內(nèi)的尸體忽然破棺而出剃允,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布斥废,位于F島的核電站椒楣,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏牡肉。R本人自食惡果不足惜捧灰,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望统锤。 院中可真熱鬧毛俏,春花似錦、人聲如沸饲窿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽逾雄。三九已至阀溶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鸦泳,已是汗流浹背银锻。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留做鹰,地道東北人击纬。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像钾麸,于是被迫代替她去往敵國和親掉弛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,037評論 2 355

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