NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
以一個為例
Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) //icon的圖片設置 .setTicker("TickerText:" + "您有新短消息,請注意查收!")//提醒的類容 .setContentTitle("Notification Title")//提醒的標題 .setContentText("This is the notification message")/提醒類容 .setContentIntent(pendingIntent3).setNumber(1).build();
PendingIntent//跳轉界面 傳遞信息用的
示例
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有一個getActivity方法,第一個參數是上下文,第二個參數 requestCode,第三個參數是 Intent,用來存儲信息,第四個參數是對參數的操作標識,常用的就是FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT。
關于第二個參數當requestCode值一樣時,后面的就會對之前的消息起作用,所以為了避免影響之前的消息,requestCode每次要設置不同的內容。
myNotify = new Notification();myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除 myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈 myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失
//DEFAULT_ALL 使用所有默認值,比如聲音,震動,閃屏等等
//DEFAULT_LIGHTS 使用默認閃光提示
//DEFAULT_SOUNDS 使用默認提示聲音
//DEFAULT_VIBRATE 使用默認手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />權限
myNotify.defaults = Notification.DEFAULT_LIGHTS;
//閃光和震動需要添加權限
<!-- 閃光燈權限 --> <uses-permission android:name= "android.permission.FlashLIGHT" > <!-- 振動器權限 --> <uses-permission android:name= "android.permission.VIBRATE" ></uses-permission></uses-permission> |
notifyBuilder.setPRogress(100, incr, false);這個設置滾動條可以寫帶滾動條的提示
/** * 帶滾動條的提示 */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:您有新下載消息,請注意查收!") .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類型是提示信息的表示重復不會彈出顯示
貼出全部代碼有的
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進行通知處理,首先需要重系統哪里獲得通知管理器NotificationManager,它是一個系統Service。 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); switch (view.getId()) { // 默認通知 case R.id.btn1: // Notification myNotify = new Notification(R.drawable.message, // "自定義通知:您有新短信息了,請注意查收!", System.currentTimeMillis()); myNotify = new Notification(); myNotify.icon = R.drawable.ic_launcher; myNotify.tickerText = "TickerText:您有新短消息,請注意查收!"; myNotify.when = System.currentTimeMillis(); myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除 myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈 myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失 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; // 默認通知 API11及之后可用 case R.id.btn2: PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 通過Notification.Builder來創建通知,注意API Level // API11之后才支持 Notification notify2 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) // 設置狀態欄中的小圖片,尺寸一般建議在24×24,這個圖片同樣也是在下拉狀態欄中所顯示,如果在那里需要更換更大的圖片,可以使用setLargeIcon(Bitmap // icon) .setTicker("TickerText:" + "您有新短消息,請注意查收!")// 設置在status // bar上顯示的提示文字 .setContentTitle("Notification Title")// 設置在下拉status // bar后Activity,本例子中的NotififyMessage的TextView中顯示的標題 .setContentText("This is the notification message")// TextView中顯示的詳細內容 .setContentIntent(pendingIntent2) // 關聯PendingIntent .setNumber(1) // 在TextView的右方顯示的數字,可放大圖片看,在最右側。這個number同時也起到一個序列號的左右,如果多個觸發多個通知(同一ID),可以指定顯示哪一個。 .getNotification(); // 需要注意build()是在API level // 16及之后增加的,在API11中可以使用getNotificatin()來代替 notify2.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(NOTIFICATION_FLAG, notify2); break; // 默認通知 API16及之后可用 case R.id.btn3: PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 通過Notification.Builder來創建通知,注意API Level // API16之后才支持 Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setTicker("TickerText:" + "您有新短消息,請注意查收!") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API // level16及之后增加的,API11可以使用getNotificatin()來替代 notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明當通知被用戶點擊時,通知將被清除。 manager.notify(NOTIFICATION_FLAG, notify3);// 步驟4:通過通知管理器來發起通知。如果id不同,則每click,在status哪里增加一個提示 break; // 自定義通知 case R.id.btn4: // Notification myNotify = new Notification(R.drawable.message, // "自定義通知:您有新短信息了,請注意查收!", System.currentTimeMillis()); myNotify = new Notification(); myNotify.icon = R.drawable.ic_launcher; myNotify.tickerText = "TickerText:您有新短消息,請注意查收!"; myNotify.when = System.currentTimeMillis(); myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除 myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈 myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失 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; //第一個參數保證信息不一樣的 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: /** * 有進度條的notification * @param view */ Pro_Notification(); break; } } NotificationCompat.Builder notifyBuilder; NotificationManager manager_n; /** * 帶滾動條的提示 */ 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:您有新下載消息,請注意查收!") .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(); }}
新聞熱點
疑難解答