با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن . و کاربرانی که دوره کامل آموزش برنامه نویسی اندروید را دنبال می کنند. notification اطلاعیه ای است که می توانید به کاربر خارج از UI معمولی برنامه خود نمایش دهید. Push Notification در اندروید به شما اجازه می دهد تا خودتان متن notification را تنظیم کنید. پیاده سازی notification در اندروید بسیار آسان است. برای این منظور اندروید کلاس NotificationManager را ارائه می دهد. برای استفاده از قابلیت های این کلاس شما باید متد getSystemService را برای ایجاد یک آبجکت از این کلاس فراخوانی کنید. برای آشنایی بیشتر با Push Notification در کدنویسی اندروید در ادامه ی مباحث این بخش با ما همراه باشید.
در این بخش ما شما را با اصول کلی پیاده سازی Push Notification در اندروید و متدهای آن آشنا خواهیم کرد.
همچنین در ادامه ی آموزش یک مثال ساده برای درک بهتر مباحث Push Notification در کدنویسی اندروید ارائه می دهیم.
همانطور که اشاره شده ابتدا بایدیک آبجکت از کلاس NotificationManager با استفاده از متد ()getSystemService بصورت زیر تعریف کنید:
1 2 | NotificationManager NM; NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); |
سپس شما می توانید صفات و ویژگی های Notification از جمله icon ، title ، time و… را تعریف کنید:
1 | Notification notify = new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis()); |
سپس باید یک PendingIntent با امکان ارسال محتوا و intent به آن در قالب پارامتر تعریف کنید:
1 | PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0); |
در نهایت باید متد setLatestEventInfo از کلاس Notification را فراخوانی کرده و pending intent ، موضوع و جزئیات اطلاعیه را ارسال کنید.
سرانجام نیز باید متد notify را از کلاس NotificationManager فراخوانی کنید:
1 2 | notify.setLatestEventInfo(getApplicationContext(), subject, body,pending); NM.notify(0, notify); |
علاوه بر متد notify ، متدهای مفید دیگری نیز از کلاس NotificationManager در دسترس است که به شرح زیر است:
ردیف | متد و توضیحات مربوطه |
---|---|
۱ | cancel(int id) این متد یک notification قبلا نشان داده شده را لغو می کند. |
۲ | cancel(String tag, int id) این متد نیز یک notification قبلا نشان داده شده را لغو می کند. |
۳ | cancelAll() این متد تمام notification های قبلا نشان داده شده را لغو می کند. |
۴ | notify(int id, Notification notification) این متد یک notification را برای نمایش در نوار status ارسال می کند. |
۵ | notify(String tag, int id, Notification notification) این متد نیز یک notification را برای نمایش در نوار status ارسال می کند. |
در ادامه به ایجاد یک اپلیکیشن ساده می پردازیم که به کاربر اجازه ی ایجاد یک notification سفارشی شده را می دهد.
شما می توانید این برنامه را در یک دستگاه اندروید واقعی و یا شبیه ساز اندروید اجرا کرده و خروجی بگیرید.
مراحل انجام کار:
مراحل | توضیحات |
---|---|
۱ | مراحل ایجاد برنامه در اندروید استدیو را طبق بخش های قبلی تحت عنوان پکیج com.example.Pvlearn.myapplication دنبال کنید. |
۲ | به فایل src/MainActivity.java کدهای notification را اضافه کنید. |
۳ | طراحی مناسب با این برنامه را به res/layout/activity_main اضافه کنید. |
۴ | برنامه را اجرا کنید تا دستگاه اندروید شما نتیجه را نمایش دهد. |
محتوای فایل MainActivity.java:
نکته: کد abc در واقع لوگوی pvlearn است.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.example.Pvlearn.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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?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="Pvlearn" 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.Pvlearn.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> |
برای اجرای برنامه ی اندرویدی خود یکی از فایل های activity در برنامه ی خود کلیک کنید.
سپس روی نماد (Run) از نوار ابزار برای شروع اجرای برنامه کلیک کنید.
صفحه اول این اپلیکیشن به صورت زیر ظاهر خواهد شد:
حالا فیلدهای مربوطه به عنوان ، موضوع و بدنه ی اطلاعیه را به صورت زیر تکمیل کنید:
حالا دکمه notify را لمس کرده و یا روی آن ضربه بزنید، سپس آیکون notification را در نوار اطلاعیه مشاهده خواهید کرد:
حالا نوار notification را به پایین scroll کنید تا notification را همانند تصویر زیر مشاهده کنید:
Push Notification در اندروید به شما اجازه می دهد تا به کاربر امکان مشخص کردن محتوا و جزئیات Notification (اطلاعیه) را بدهید. این قابلیت می تواند در توسعه برخی اپلیکیشن های اندروید مفید باشد.
مباحث بخش بعدی را به RenderScript در اندروید اختصاص خواهیم داد.