بناء تطبيق إحترافى يدعم الإشعارات بإحترافية عبر الاندرويد ستوديوالاصدار الحديث
اولاً جميعاً نعلم ان الاشعارات تجعل من التطبيق حياً على الهاتف و تجعله مميزاً على الاقل دوناً عن باقى التطبيقات و فى هذا الدرس سنقوم بعمل تطبيق إحترافى يدعم الإشعارات بطريقة مميزة .
نقوم بفتح اندرويد ستوديو و نقوم بعمل مشروع جديد
يفضل العمل على اَخر إصدار من اندرويد ستوديو التحميل من هنا
وهو يدعم الاَتى :
SDK Build Tools 26.0.1
SDK Platform Tools 26.0.0
Android Emulator 26.1.4
Android Support Library 26.0.2
The Android O SDK
بعدما نقوم بعمل المشروع الجديد نقوم بالدخول الى activity_main.xml
و نقوم بلصق الكود التالى
..................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:id="@+id/activity_main">
<EditText
android:id="@+id/channel_one_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/channel_one_text"/>
<Button
android:id="@+id/post_to_channel_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_label" />
<Button
android:id="@+id/channel_one_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/settings" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:id="@+id/activity_main">
<EditText
android:id="@+id/channel_one_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/channel_one_text"/>
<Button
android:id="@+id/post_to_channel_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_label" />
<Button
android:id="@+id/channel_one_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/settings" />
....................................................................
<EditText
android:id="@+id/channel_two_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/channel_two_text"/>
<Button
android:id="@+id/post_to_channel_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_label" />
<Button
android:id="@+id/channel_two_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/settings" />
</LinearLayout>
android:id="@+id/channel_two_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/channel_two_text"/>
<Button
android:id="@+id/post_to_channel_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_label" />
<Button
android:id="@+id/channel_two_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/settings" />
</LinearLayout>
.................................................
ونقوم ايضاً بإضافة الكود التالى
..................................................
<resources>
<string name="app_name">Notification Channels</string>
<string name="channel_one_text">Channel One</string>
<string name="channel_two_text">Channel Two</string>
<string name="button_label">Post Notification</string>
<string name="settings">Settings</string>
<string name="channel_one_body">Notification Body Text</string>
<string name="channel_two_body">Notification Body Text</string>
</resources>
<string name="app_name">Notification Channels</string>
<string name="channel_one_text">Channel One</string>
<string name="channel_two_text">Channel Two</string>
<string name="button_label">Post Notification</string>
<string name="settings">Settings</string>
<string name="channel_one_body">Notification Body Text</string>
<string name="channel_two_body">Notification Body Text</string>
</resources>
................................................
سيصبح الان شكل التطبيق مبدئياً هكذا
نقوم بالضغط على New ثم Java Class و نقوم بتسمية هذا الكلاس بالأسم التالى "NotificationHelper"
ثم نقوم بكتابة كود الجافا التالى
.......................................
class NotificationHelper extends ContextWrapper {
private NotificationManager notifManager;
//Set the channel’s ID//
public static final String CHANNEL_ONE_ID = "com.jessicathornsby.myapplication.ONE";
//Set the channel’s user-visible name//
public static final String CHANNEL_ONE_NAME = "Channel One";
...
...
...
private NotificationManager notifManager;
//Set the channel’s ID//
public static final String CHANNEL_ONE_ID = "com.jessicathornsby.myapplication.ONE";
//Set the channel’s user-visible name//
public static final String CHANNEL_ONE_NAME = "Channel One";
...
...
...
...................................................................
// Create the channel object, using the channel ID//
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, notifManager.IMPORTANCE_HIGH);
//Configure the channel’s initial settings//
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
......................................................................
ثم نضيف هذا الكود
.....................................................................
getManager().createNotificationChannel(notificationChannel);
...................................................................
public Notification.Builder getNotification1(String title, String body)
//Pass the notification channel’s ID as the second argument//
return new Notification.Builder(getApplicationContext(), CHANNEL_ONE_ID)
//Set the notification’s title, which in this instance will be the contents of our EditText//
.setContentTitle(title)
//Set the notification’s body text//
.setContentText(body)
//Set the notification’s icon, which we’ll be creating later//
.setSmallIcon(R.drawable.warning)
.setAutoCancel(true);
}
//Pass the notification channel’s ID as the second argument//
return new Notification.Builder(getApplicationContext(), CHANNEL_ONE_ID)
//Set the notification’s title, which in this instance will be the contents of our EditText//
.setContentTitle(title)
//Set the notification’s body text//
.setContentText(body)
//Set the notification’s icon, which we’ll be creating later//
.setSmallIcon(R.drawable.warning)
.setAutoCancel(true);
}
................................................................
ثم نضيف الكود هذا ايضاً
............................................................
package com.jessicathornsby.myapplication;
import android.graphics.Color;
import android.graphics.Color;
.........................................................
mport android.content.Context;
import android.content.ContextWrapper;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
class NotificationHelper extends ContextWrapper {
private NotificationManager notifManager;
public static final String CHANNEL_ONE_ID = "com.jessicathornsby.myapplication.ONE";
public static final String CHANNEL_ONE_NAME = "Channel One";
public static final String CHANNEL_TWO_ID = "com.jessicathornsby.myapplication.TWO";
public static final String CHANNEL_TWO_NAME = "Channel Two";
//Create your notification channels//
public NotificationHelper(Context base) {
super(base);
createChannels();
}
public void createChannels() {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, notifManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);
NotificationChannel notificationChannel2 = new NotificationChannel(CHANNEL_TWO_ID,
CHANNEL_TWO_NAME, notifManager.IMPORTANCE_DEFAULT);
notificationChannel2.enableLights(false);
notificationChannel2.enableVibration(true);
notificationChannel2.setLightColor(Color.RED);
notificationChannel2.setShowBadge(false);
getManager().createNotificationChannel(notificationChannel2);
}
//Create the notification that’ll be posted to Channel One//
import android.content.ContextWrapper;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
class NotificationHelper extends ContextWrapper {
private NotificationManager notifManager;
public static final String CHANNEL_ONE_ID = "com.jessicathornsby.myapplication.ONE";
public static final String CHANNEL_ONE_NAME = "Channel One";
public static final String CHANNEL_TWO_ID = "com.jessicathornsby.myapplication.TWO";
public static final String CHANNEL_TWO_NAME = "Channel Two";
//Create your notification channels//
public NotificationHelper(Context base) {
super(base);
createChannels();
}
public void createChannels() {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, notifManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);
NotificationChannel notificationChannel2 = new NotificationChannel(CHANNEL_TWO_ID,
CHANNEL_TWO_NAME, notifManager.IMPORTANCE_DEFAULT);
notificationChannel2.enableLights(false);
notificationChannel2.enableVibration(true);
notificationChannel2.setLightColor(Color.RED);
notificationChannel2.setShowBadge(false);
getManager().createNotificationChannel(notificationChannel2);
}
//Create the notification that’ll be posted to Channel One//
..................................................................
public Notification.Builder getNotification1(String title, String body) {
return new Notification.Builder(getApplicationContext(), CHANNEL_ONE_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.warning)
.setAutoCancel(true);
}
//Create the notification that’ll be posted to Channel Two//
public Notification.Builder getNotification2(String title, String body) {
return new Notification.Builder(getApplicationContext(), CHANNEL_TWO_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.alert)
.setAutoCancel(true);
}
public void notify(int id, Notification.Builder notification) {
getManager().notify(id, notification.build());
}
//Send your notifications to the NotificationManager system service//
private NotificationManager getManager() {
if (notifManager == null) {
notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return notifManager;
}
}
return new Notification.Builder(getApplicationContext(), CHANNEL_ONE_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.warning)
.setAutoCancel(true);
}
//Create the notification that’ll be posted to Channel Two//
public Notification.Builder getNotification2(String title, String body) {
return new Notification.Builder(getApplicationContext(), CHANNEL_TWO_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.alert)
.setAutoCancel(true);
}
public void notify(int id, Notification.Builder notification) {
getManager().notify(id, notification.build());
}
//Send your notifications to the NotificationManager system service//
private NotificationManager getManager() {
if (notifManager == null) {
notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return notifManager;
}
}
...................................................
نقوم بعمل تجربة للتطبيق
ليست هناك تعليقات:
إرسال تعليق