Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > Windows Azure > Windows Azure (Mobile Services) and Android > ตอนที่ 10 : การทำ Push Notifications in Mobile Services ด้วย Android



Clound SSD Virtual Server

ตอนที่ 10 : การทำ Push Notifications in Mobile Services ด้วย Android

ตอนที่ 10 : การทำ Push Notifications in Mobile Services ด้วย Android การทำ Push Notification เป็นความสามารถหนึ่งของ Mobile Services ในการ Push Alert แจ้งไปยังเครื่อง Android Client ที่ติดตั้ง App ที่เราเขียนขึ้น และถ้าเราใช้ Android กับ Mobile Services ซึ่ง Android นั้นเป็นของ Google จึงจะต้องใช้บริการของ Google Cloud Messaging (GCM) ทำงานคู่กับ Mobile Services ของ Windows Azure และก่อนการใช้งานจะต้อทำการ Register บริการที่ Google APIs ของ Google ซะก่อน

บทความนี้จะเป็นตัวอย่างที่ต่อยอดมาจาก Code ของตอนที่ 3


สมัครบริการ Google APIs
https://code.google.com/apis/console/b/0/


Android Push Notifications in Mobile Services

หน้าจอสำหรับการสมัคร (ในปัจจุบันหน้าตาอาจจะเปลี่ยนไปบ้าง แต่การสมัครจะคล้าย ๆ กัน)

Android Push Notifications in Mobile Services

ภายใต้ API Access -> Create new Server key...

Android Push Notifications in Mobile Services

เลือก Create

Android Push Notifications in Mobile Services

ตอนนี้เราจะได้ API key สำหรับใช้งานแล้ว








กลับมายังหน้า Portal Management บน Mobile Services

Android Push Notifications in Mobile Services

เลือก Mobile Services ที่เราได้สร้างไว้ก่อนหน้านี้

Android Push Notifications in Mobile Services

เลือกเมนู PUSH และกรอก API Key ที่ได้จากการสมัครบน Google APIs

กลับมายังหน้า App ของเราบนโปรแกรม Eclipse

Android Push Notifications in Mobile Services

ในส่วนของ Android SDK ให้ทำการดาวน์โหลดตัวที่ชื่อว่า "Google Cloud Messaging for Android Library"

จากนั้นให้ Browse ตัว SDK เลือกไฟล์ gcm.jar จาก \extras\google\gcm\gcm-client\dist ไปเก็บไว้ใน \libs ของ Project จากนั้นให้คลิกขวาที่ libs แล้ว Refresh ซะ 1 ครั้ง

ในส่วนของไฟล์ AndroidManifest.xml ให้เพิ่ม Tags ดังต่อไปนี้

ภายใต้ uses-permission
<permission android:name="**my_app_package**.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="**my_app_package**.permission.C2D_MESSAGE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


ภายใต้ application
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="**my_app_package**" />
    </intent-filter>
</receiver>
<service android:name=".GCMIntentService" />


เปิดไฟล์ ToDoItem.java แล้วเพิ่ม Code นี้ลงไป

@com.google.gson.annotations.SerializedName("channel")
private String mRegistrationId;


public String getRegistrationId() {
    return mRegistrationId;
}


public final void setRegistrationId(String registrationId) {
    mRegistrationId = registrationId;
}


เปิดไฟล์ ToDoItemActivity.java

import com.google.android.gcm.GCMRegistrar;


กำหนดส่วนของ <SENDER_ID>

private String mRegistationId;
public static final String SENDER_ID = "<SENDER_ID>";


ภายใต้ onCreate ให้เพิ่ม

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
mRegistationId = GCMRegistrar.getRegistrationId(this);
if (mRegistationId.equals("")) {
    GCMRegistrar.register(this, SENDER_ID);
}


ในส่วนของ addItem เพิ่ม

item.setRegistrationId(mRegistationId.equals("") ?
    GCMIntentService.getRegistrationId() : mRegistationId);


จากนั้นให้คลิกขวาที่ Project เลือกสร้าง Class ขึ้นมาใหม่

Android Push Notifications in Mobile Services

กำหนดชื่อว่า GCMBaseIntentService

ให้ import library นี้ลงไปใน Class

import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;


ใน Class เพิ่มคำสั่งนี้

private static String sRegistrationId;


public static String getRegistrationId() {
    return sRegistrationId;
}


public GCMIntentService(){
    super(ToDoActivity.SENDER_ID);
}


และ

@Override
protected void onMessage(Context context, Intent intent) {


NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New todo item!")
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentText(intent.getStringExtra("message"));
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());


}


@Override
protected void onRegistered(Context context, String registrationId) {
    sRegistrationId = registrationId;
}









กลับมายังหน้า Portal Management ของ Mobile Services

Android Push Notifications in Mobile Services

ไปที่ DATA และเลือก Table ที่ได้สร้างไว้

Android Push Notifications in Mobile Services

ภายใต้ Script ของคำสั่ง insert()

แก้ไขเป็น

function insert(item, user, request) {
    request.execute({
        success: function() {
            // Write to the response and then send the notification in the background
            request.respond();
            push.gcm.send(item.channel, item.text, {
                success: function(response) {
                    console.log('Push notification sent: ', response);
                }, error: function(error) {
                    console.log('Error sending push notification: ', error);
                }
            });
        }
    });
}



กลับมาที่ Project บน Eclipse

Android Push Notifications in Mobile Services

ให้คลิกขวาที่ Project เลือก Properties ในส่วนของ Android -> เลือก Google APIs

Android Push Notifications in Mobile Services

และให้สร้าง Emulator ขึ้นมาใหม่ด้วย

Android Push Notifications in Mobile Services

เลือกเป็น Google APIs (Google Inc.) - API Level 17

Android Push Notifications in Mobile Services

จากนั้นทดสอบทดสอบรัน Emulator และทดสอบเพิ่มข้อมูล

Android Push Notifications in Mobile Services

หลังจากที่เพิ่มข้อมูลแล้ว ก็จะมีการ Push แจ้งไปยัง Android Client ทุก ๆ เครื่องที่ติดตั้ง App ของเรา

สำหรับขั้นตอนทั้งหมดนี้อาจจะมีความซับซ้อนนิดหนึ่ง แต่ก็ไม่ยากที่จะค่อย ๆ ไล่ทำตามตัวอย่าง หรือไม่ลองอ่านบทความนี้เพิ่มเติม

Get started with push notifications in Mobile Services



บทความถัดไปที่แนะนำให้อ่าน




บทความที่เกี่ยวข้อง


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-09 15:38:43 / 2017-03-24 11:16:28
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก Android Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียมความพร้อมของ Android ก่อนที่จะเขียน Android กับ Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง Android Mobile Services และการเรียกใช้งานแบบง่าย ๆ
Rating :

 
ตอนที่ 4 : การเชื่อมต่อ Android กับ Mobile Services บน Windows Azure
Rating :

 
ตอนที่ 5 : สร้าง Project บน Eclipse การเชื่อมต่อไปยัง Mobile Services ด้วย Android
Rating :

 
ตอนที่ 6 : Android สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล
Rating :

 
ตอนที่ 7 : Android อ่าน Data จาก Table ของ Mobile Services และแสดงผลบน App
Rating :

 
ตอนที่ 8 : อ่านข้อมูล Mobile Services แบบมีเงื่อนไข Where และแสดงผลบน Android
Rating :

 
ตอนที่ 9 : การทำ Authentication in Azure Mobile Services ด้วย Android
Rating :

 
ตอนที่ 11 : การทำ Validate และ Modify data in Mobile Services บน Andriod
Rating :

 
ตอนที่ 12 : การทำ Refine Mobile Services queries with paging บน Android
Rating :

 
ตอนที่ 13 : การเขียน Scripts to authorize users in Mobile Services บน Android
Rating :

 
ตอนที่ 14 : Show Case 1 : Register Form (Android and Mobile Services)
Rating :

 
ตอนที่ 15 : Show Case 2 : Login User Password (Android and Mobile Services)
Rating :

 
ตอนที่ 16 : Show Case 3 : Update Data (Android and Mobile Services)
Rating :

 
ตอนที่ 17 : Show Case 4 : Delete Data (Android and Mobile Services)
Rating :

 
ตอนที่ 18 : บทความอื่น ๆ เกี่ยวกับ Android Mobile Services บน Windows Azure
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่