NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
以一個(gè)為例
Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) //icon的圖片設(shè)置 .setTicker("TickerText:" + "您有新短消息,請(qǐng)注意查收!")//提醒的類(lèi)容 .setContentTitle("Notification Title")//提醒的標(biāo)題 .setContentText("This is the notification message")/提醒類(lèi)容 .setContentIntent(pendingIntent3).setNumber(1).build();
PendingIntent//跳轉(zhuǎn)界面 傳遞信息用的
示例
intent1 = new Intent(getapplicationContext(), ContentActivity.class); intent1.putExtra("content", "444"); intent1.putExtra("number", "444"); intent1.putExtra("date", "444"); PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent有一個(gè)getActivity方法,第一個(gè)參數(shù)是上下文,第二個(gè)參數(shù) requestCode,第三個(gè)參數(shù)是 Intent,用來(lái)存儲(chǔ)信息,第四個(gè)參數(shù)是對(duì)參數(shù)的操作標(biāo)識(shí),常用的就是FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT。
關(guān)于第二個(gè)參數(shù)當(dāng)requestCode值一樣時(shí),后面的就會(huì)對(duì)之前的消息起作用,所以為了避免影響之前的消息,requestCode每次要設(shè)置不同的內(nèi)容。
myNotify = new Notification();myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動(dòng)清除 myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認(rèn)設(shè)置,比如鈴聲、震動(dòng)、閃燈 myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點(diǎn)擊消息后,消息自動(dòng)在通知欄自動(dòng)消失
//DEFAULT_ALL 使用所有默認(rèn)值,比如聲音,震動(dòng),閃屏等等
//DEFAULT_LIGHTS 使用默認(rèn)閃光提示
//DEFAULT_SOUNDS 使用默認(rèn)提示聲音
//DEFAULT_VIBRATE 使用默認(rèn)手機(jī)震動(dòng),需加上<uses-permission android:name="android.permission.VIBRATE" />權(quán)限
myNotify.defaults = Notification.DEFAULT_LIGHTS;
//閃光和震動(dòng)需要添加權(quán)限
<!-- 閃光燈權(quán)限 --> <uses-permission android:name= "android.permission.FlashLIGHT" > <!-- 振動(dòng)器權(quán)限 --> <uses-permission android:name= "android.permission.VIBRATE" ></uses-permission></uses-permission> |
notifyBuilder.setPRogress(100, incr, false);這個(gè)設(shè)置滾動(dòng)條可以寫(xiě)帶滾動(dòng)條的提示
/** * 帶滾動(dòng)條的提示 */public void Pro_Notification() { manager_n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notifyBuilder = new NotificationCompat.Builder(this); notifyBuilder.setContentTitle("Picture Download") .setContentText("Download in progress") .setTicker("TickerText:您有新下載消息,請(qǐng)注意查收!") .setOngoing(true) .setSmallIcon(R.drawable.ic_launcher); // Start a lengthy Operation in a background thread new Thread( new Runnable() { @Override public void run() { int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr += 5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state notifyBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. manager_n.notify(0, notifyBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep( 1000); } catch (InterruptedException e) { Log.d("NOTIFICATION", "sleep failure"); } } // When the loop is finished, updates the notification notifyBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0, 0, false); manager_n.notify(213, notifyBuilder.build()); } } // Starts the thread by calling the run() method in its Runnable ).start();}manager.notify(NOTIFICATION_FLAG, notify2);中的NOTIFICATION_FLAG是int類(lèi)型是提示信息的表示重復(fù)不會(huì)彈出顯示
貼出全部代碼有的
package com.yundong.pushmessage;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.app.NotificationCompat;import android.util.Log;import android.view.View;import android.widget.RemoteViews;public class MainActivity extends AppCompatActivity { private static final int NOTIFICATION_FLAG = 1; private static final int NOTIFICATION_FLAG_4 = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void notificationMethod(View view) { Notification myNotify; RemoteViews rv; Intent intent1; PendingIntent contentIntent; // 在Android進(jìn)行通知處理,首先需要重系統(tǒng)哪里獲得通知管理器NotificationManager,它是一個(gè)系統(tǒng)Service。 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); switch (view.getId()) { // 默認(rèn)通知 case R.id.btn1: // Notification myNotify = new Notification(R.drawable.message, // "自定義通知:您有新短信息了,請(qǐng)注意查收!", System.currentTimeMillis()); myNotify = new Notification(); myNotify.icon = R.drawable.ic_launcher; myNotify.tickerText = "TickerText:您有新短消息,請(qǐng)注意查收!"; myNotify.when = System.currentTimeMillis(); myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動(dòng)清除 myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認(rèn)設(shè)置,比如鈴聲、震動(dòng)、閃燈 myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點(diǎn)擊消息后,消息自動(dòng)在通知欄自動(dòng)消失 rv = new RemoteViews(getPackageName(), R.layout.my_notification); rv.setTextViewText(R.id.text_content, "hello wrold!"); myNotify.contentView = rv;// Intent intent = new Intent(Intent.ACTION_MAIN); intent1 = new Intent(getApplicationContext(), ContentActivity.class); intent1.putExtra("content", "111"); intent1.putExtra("number", "111"); intent1.putExtra("date", "111"); contentIntent = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT); myNotify.contentIntent = contentIntent; manager.notify(NOTIFICATION_FLAG, myNotify); break; // 默認(rèn)通知 API11及之后可用 case R.id.btn2: PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 通過(guò)Notification.Builder來(lái)創(chuàng)建通知,注意API Level // API11之后才支持 Notification notify2 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) // 設(shè)置狀態(tài)欄中的小圖片,尺寸一般建議在24×24,這個(gè)圖片同樣也是在下拉狀態(tài)欄中所顯示,如果在那里需要更換更大的圖片,可以使用setLargeIcon(Bitmap // icon) .setTicker("TickerText:" + "您有新短消息,請(qǐng)注意查收!")// 設(shè)置在status // bar上顯示的提示文字 .setContentTitle("Notification Title")// 設(shè)置在下拉status // bar后Activity,本例子中的NotififyMessage的TextView中顯示的標(biāo)題 .setContentText("This is the notification message")// TextView中顯示的詳細(xì)內(nèi)容 .setContentIntent(pendingIntent2) // 關(guān)聯(lián)PendingIntent .setNumber(1) // 在TextView的右方顯示的數(shù)字,可放大圖片看,在最右側(cè)。這個(gè)number同時(shí)也起到一個(gè)序列號(hào)的左右,如果多個(gè)觸發(fā)多個(gè)通知(同一ID),可以指定顯示哪一個(gè)。 .getNotification(); // 需要注意build()是在API level // 16及之后增加的,在API11中可以使用getNotificatin()來(lái)代替 notify2.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(NOTIFICATION_FLAG, notify2); break; // 默認(rèn)通知 API16及之后可用 case R.id.btn3: PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 通過(guò)Notification.Builder來(lái)創(chuàng)建通知,注意API Level // API16之后才支持 Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setTicker("TickerText:" + "您有新短消息,請(qǐng)注意查收!") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API // level16及之后增加的,API11可以使用getNotificatin()來(lái)替代 notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明當(dāng)通知被用戶點(diǎn)擊時(shí),通知將被清除。 manager.notify(NOTIFICATION_FLAG, notify3);// 步驟4:通過(guò)通知管理器來(lái)發(fā)起通知。如果id不同,則每click,在status哪里增加一個(gè)提示 break; // 自定義通知 case R.id.btn4: // Notification myNotify = new Notification(R.drawable.message, // "自定義通知:您有新短信息了,請(qǐng)注意查收!", System.currentTimeMillis()); myNotify = new Notification(); myNotify.icon = R.drawable.ic_launcher; myNotify.tickerText = "TickerText:您有新短消息,請(qǐng)注意查收!"; myNotify.when = System.currentTimeMillis(); myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動(dòng)清除 myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認(rèn)設(shè)置,比如鈴聲、震動(dòng)、閃燈 myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點(diǎn)擊消息后,消息自動(dòng)在通知欄自動(dòng)消失 rv = new RemoteViews(getPackageName(), R.layout.my_notification); rv.setTextViewText(R.id.text_content, "hello wrold!"); myNotify.contentView = rv;// Intent intent = new Intent(Intent.ACTION_MAIN); intent1 = new Intent(getApplicationContext(), ContentActivity.class); intent1.putExtra("content", "444"); intent1.putExtra("number", "444"); intent1.putExtra("date", "444"); contentIntent = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT); myNotify.contentIntent = contentIntent; //第一個(gè)參數(shù)保證信息不一樣的 manager.notify(NOTIFICATION_FLAG_4, myNotify); break; case R.id.btn5: // 清除id為NOTIFICATION_FLAG的通知// manager.cancel(NOTIFICATION_FLAG);// manager.cancel(NOTIFICATION_FLAG_4); // 清除所有的通知 manager.cancelAll(); break; case R.id.btn6: Pro_Notification(); break; default: /** * 有進(jìn)度條的notification * @param view */ Pro_Notification(); break; } } NotificationCompat.Builder notifyBuilder; NotificationManager manager_n; /** * 帶滾動(dòng)條的提示 */ public void Pro_Notification() { manager_n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notifyBuilder = new NotificationCompat.Builder(this); notifyBuilder.setContentTitle("Picture Download") .setContentText("Download in progress") .setTicker("TickerText:您有新下載消息,請(qǐng)注意查收!") .setOngoing(true) .setSmallIcon(R.drawable.ic_launcher); // Start a lengthy operation in a background thread new Thread( new Runnable() { @Override public void run() { int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr += 5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state notifyBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. manager_n.notify(0, notifyBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep( 1000); } catch (InterruptedException e) { Log.d("NOTIFICATION", "sleep failure"); } } // When the loop is finished, updates the notification notifyBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0, 0, false); manager_n.notify(213, notifyBuilder.build()); } } // Starts the thread by calling the run() method in its Runnable ).start(); }}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注