Android-push-notification

提供:Dev Guides
移動先:案内検索

Android-プッシュ通知

通知は、アプリケーションの通常のUIの外部でユーザーに表示できるメッセージです。 Androidで独自の通知を非常に簡単に作成できます。

Androidは、この目的のために NotificationManager クラスを提供します。 このクラスを使用するには、* getSystemService()メソッド*を使用してAndroidシステムに要求することにより、このクラスのオブジェクトをインスタンス化する必要があります。 その構文は以下のとおりです-

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

その後、 Notification クラスを使用してNotificationを作成し、アイコン、タイトル、時間などの属性を指定しますe.t.c. その構文は以下のとおりです-

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

次に行う必要があるのは、コンテキストとインテントをパラメーターとして渡して PendingIntent を作成することです。 PendingIntentを別のアプリケーションに与えることで、他のアプリケーションが自分であるかのように、指定した操作を実行する権利をそれに付与します。

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

最後に行う必要があるのは、Notificationクラスの setLatestEventInfo メソッドを呼び出し、通知の件名と本文の詳細とともに保留中のインテントを渡すことです。 その構文は次のとおりです。 そして、最後にNotificationManagerクラスのnotifyメソッドを呼び出します。

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

notifyメソッドとは別に、NotificationManagerクラスで使用できる他のメソッドがあります。 それらは以下にリストされています-

Sr.No Method & description
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クラスの使用法を示しています。 通知を作成できる基本的なアプリケーションを作成します。

この例を試すには、実際のデバイスまたはエミュレーターでこれを実行する必要があります。

Steps Description
1 You will use Android studio to create an Android application under a packagecom.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add Notification code.
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4 Run the application and choose a running android device and install the application on it and verify the results.
*MainActivity.java* のコンテンツは次のとおりです。

'_次のコードで abc はfinddevguides.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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。

Androidプッシュ通知チュートリアル

ここで、タイトル、件名、本文をフィールドに入力します。 これは以下の図に示されています-

Androidプッシュ通知チュートリアル

通知ボタンをクリックすると、上部の通知バーに通知が表示されます。 以下に示されています-

Androidプッシュ通知チュートリアル

通知バーを下にスクロールして、通知を確認します。 これは以下の図に示されています-

Androidプッシュ通知チュートリアル