مشخصات مقاله
-
2884
-
0.0
-
5660
-
0
-
0
آموزش اطلاع رسانی (notification) در اندروید
استفاده از notification در اندروید
این مقاله ی آموزشی نحوه ی استفاده از Notification Manager در اندروید را به شما آموزش می دهد .
فهرست محتوا
1. Notification manager چیست ؟
- چگونگی راه اندازی notification ها
- نحوه ی لغو notification ها
2. Pending intent
3. مثال : notification manager
notification manager چیست ؟
اندروید این اجازه را به شما می دهد که notification (اطلاعیه) ها را در نوارعنوان (title bar) برنامه ی خود قرار دهید . کاربر می تواند notification bar را بزرگ کرده و با انتخاب notification, یک activity دیگر را فعال و راه اندازی کند .
از آنجایی که notification ها چندان خوشایند کاربرها نیستند, کاربر می تواند به راحتی با مراجعه به تنظیمات (Settings) اپلیکیشن دستگاه اندروید, notification هر برنامه را غیرفعال کند . کافی است در بخش تنظیمات گزینه ی Apps را انتخاب کرده, سپس برنامه ی دلخواه را انتخاب کنید . اکنون با غیرفعال کردن کادر تیک (checkbox) Show notifications از نمایش اطلاعیه های مربوط به آن برنامه جلوگیری کنید .
چگونگی راه اندازی notification ها
Notification ها در اندروید در قالب کلاس Notification ارائه و نمایش داده می شوند. به منظور ایجاد notifications شما می توانید از کلاس NotificationManager بهره بگیرید که با استفاده از متد getSystemService() از Context مشتق و دریافت شده, برای مثال می توان به یک activity یا service اشاره کرد .
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder جهت ایجاد شئ Notification, یک interfacebuilder (سازنده رابط) عرضه می کند . برای تعیین عملیاتی که بلافاصله پس از انتخابnotification توسط کاربر اجرا می شود, لازم است از PendingIntent بهره گرفت .
Notification.Builder به شما اجازه می دهد تا سه دکمه با عملیات تعریف پذیر به notification مربوطه اضافه کنید.
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
ویرایش 4.1 اندروید از قابلیت notification های بسط پذیر پشتیبانی می کند . علاوه بر notification view های معمول می توان یک view بزرگ تعریف کرد که هنگامی نمایش داده می شود که notification مورد نظر بسط داده شود . سه سبک یا استایل کلی وجود دارد که باید با view بزرگ بکار گرفته شود: big picture style, big text style, inbox style
کد زیر کاربرد تابع BigTextStyle() را نمایش می دهد که اجازه ی استفاده تا 256 dp را می دهد :
String longText = "..."; Notification noti = new Notification.Builder(this). ..... .setStyle(new Notification.BigTextStyle().bigText(longText))
نحوه ی لغو notification ها
کاربر می تواند تمامی notification ها را رد یا کنسل کند و یا در صورت تنظیم notification به حالت auto-cancel, مجرد اینکه کاربر notification را انتخاب کرد, notification خود کنسل می شود .
همچنین می توان تابع cancel() را برای شناسه ی (ID) notification در NotificationManager فراخوانی کرد . متد cancelAll() کلیه ی notification هایی که شما قبلاً صادر کرده بودید را حذف می کند .
Pending intent
نشانه (token) ای است که شما به برنامه ی دیگری می دهید (برای مثال notification manager, alarm manager) که به برنامه ی مذکور این امکان را می دهد با استفاده از مجوزهای برنامه ی شما یک تکه کد از پیش تعریف شده را اجرا کند .
جهت اجرای broadcast (عملیات پخش) به وسیله ی pending intent, کافی است با فراخوانی متد getBroadcast() یک pending intent از کلاس PendingIntent دریافت کنید . به منظور راه اندازی یک activity از طریق pending intent, activity مورد نظر را با صدا زدن تابع PendingIntent.getActivity() دریافت کنید .
مثال : notification manager
پروژه ی جدیدی به نام de.vogella.android.notificationmanager ایجاد کرده, سپس به ساخت activity پرداخته و آن را CreateNotificationActivity نام گذاری کنید . این activity باید فایل طرح بندی main.xml را مورد استفاده قرار دهد .
فایل طرح بندی result.xml را ایجاد کنید .
حال activity دیگری به نام NotificationReceiverActivity با کنویسی زیر ایجاد کنید . از ثبت activity در فایل AndroidManfest.mf اطمینان حاصل کنید .
package de.vogella.android.notificationmanager;
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiverActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
}
CreateNotificationActivity را به کدینگ زیر تغییر دهید.
package de.vogella.android.notificationmanager;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class CreateNotificationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}