• Android Video Tutorials

Android - 推送通知



通知是在應用程式正常 UI 之外顯示給使用者的訊息。您可以在 Android 中非常輕鬆地建立自己的通知。

Android 為此目的提供了NotificationManager 類。為了使用此類,您需要透過getSystemService() 方法請求 Android 系統來例項化此類的物件。其語法如下所示:

NotificationManager NM;
NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

之後,您將透過Notification 類建立通知並指定其屬性,例如圖示、標題和時間等。其語法如下所示:

Notification notify = new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());

接下來需要做的是透過傳遞上下文和意圖作為引數來建立PendingIntent。透過向另一個應用程式提供 PendingIntent,您授予它執行您指定的 operatio 的許可權,就好像另一個應用程式是自己一樣。

PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0);

最後需要做的是呼叫 Notification 類的setLatestEventInfo 方法,並將 pending intent 與通知主題和正文詳細資訊一起傳遞。其語法如下所示。然後最後呼叫 NotificationManager 類的 notify 方法。

notify.setLatestEventInfo(getApplicationContext(), subject, body,pending);
NM.notify(0, notify);		

除了 notify 方法之外,NotificationManager 類中還提供了其他方法。它們列在下面:

序號 方法和描述
1

cancel(int id)

此方法取消先前顯示的通知。

2

cancel(String tag, int id)

此方法也取消先前顯示的通知。

3

cancelAll()

此方法取消所有先前顯示的通知。

4

notify(int id, Notification notification)

此方法釋出要在狀態列中顯示的通知。

5

notify(String tag, int id, Notification notification)

此方法還在狀態列中釋出要顯示的通知。

示例

以下示例演示了 NotificationManager 類的用法。它建立了一個基本的應用程式,允許您建立通知。

要試驗此示例,您需要在實際裝置或模擬器上執行它。

步驟 描述
1 您將使用 Android Studio 在包 com.example.sairamkrishna.myapplication 下建立一個 Android 應用程式。
2 修改 src/MainActivity.java 檔案以新增通知程式碼。
3 修改佈局 XML 檔案 res/layout/activity_main.xml,如果需要新增任何 GUI 元件。
4 執行應用程式並選擇一個正在執行的 Android 裝置,並在其上安裝應用程式並驗證結果。

以下是MainActivity.java的內容。

在以下程式碼中,abc表示 tutorialspoint.com 的徽標
package com.example.sairamkrishna.myapplication;

import android.app.Notification;
import android.app.NotificationManager;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
   EditText ed1,ed2,ed3;
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      ed1=(EditText)findViewById(R.id.editText);
      ed2=(EditText)findViewById(R.id.editText2);
      ed3=(EditText)findViewById(R.id.editText3);
      Button b1=(Button)findViewById(R.id.button);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String tittle=ed1.getText().toString().trim();
            String subject=ed2.getText().toString().trim();
            String body=ed3.getText().toString().trim();

            NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify=new Notification.Builder
               (getApplicationContext()).setContentTitle(tittle).setContentText(body).
               setContentTitle(subject).setSmallIcon(R.drawable.abc).build();
                
               notify.flags |= Notification.FLAG_AUTO_CANCEL;
               notif.notify(0, notify);
         }
      });
   }
}

以下是activity_main.xml的內容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notification"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      .
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_below="@+id/textView2"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_marginTop="52dp"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"
      android:hint="Name" />
   
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:hint="Subject"
      android:layout_below="@+id/editText"
      android:layout_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="textPersonName"
      android:ems="10"
      android:id="@+id/editText3"
      android:hint="Body"
      android:layout_below="@+id/editText2"
      android:layout_alignLeft="@+id/editText2"
      android:layout_alignStart="@+id/editText2"
      android:layout_alignRight="@+id/editText2"
      android:layout_alignEnd="@+id/editText2" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notification"
      android:id="@+id/button"
      android:layout_marginTop="77dp"
      android:layout_below="@+id/editText3"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />

</RelativeLayout>

以下是AndroidManifest.xml的內容。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

讓我們嘗試執行我們的應用程式。要從 Android Studio 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的執行Eclipse Run Icon圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇要在其中執行 Android 應用程式的選項。

Android Push Notification Tutorial

現在填寫標題、主題和正文欄位。這已在下圖中顯示:

Android Push Notification Tutorial

現在單擊通知按鈕,您將在頂部通知欄中看到一個通知。這已在下圖中顯示:

Android Push Notification Tutorial

現在向下滾動通知欄並檢視通知。這已在下圖中顯示:

Android Push Notification Tutorial
廣告

© . All rights reserved.