Bài 50: Cách Sử Dụng Google Cloud Messaging Là Gì ? Firebase Cloud Messaging Là Gì

Related Articles

Google Cloud Messaging cho Android (GCM) là một dịch vụ cho phép gửi dữ liệu từ những máy chủ đến ứng dụng android.Sử dụng dịch vụ này, bạn có thể gửi dữ liệu cho ứng dụng của bạn bất cứ khi nào có dữ liệu mới một cách tự động thay vìphải gửi yêu cầu đến máy chủ.

Sơ đồ dưới đây là minh họa tổng quan về mục tiêu của mỗi thực thể tham gia trong hướng dẫn google cloud messaging cho android .

Bạn đang xem: Google cloud messaging là gì

*

Đầu tiên thiết bị android gửi sender idapplication id đến máy chủ GCM để đăng kýSau khi đăng ký thành công máy chủ GCM sẽ phát sinh một registration id cho thiết bị androidSau khi nhậnregistration id,thiết bị sẽ gửiregistration idđến serverServer sẽ lưuregistration idvào cơ sở dữ liệuBất cứ khi nào cóthông báo, server sẽ gửi một thông điệp tới máy chủ GCM cùng với registration id củathiết bịMáy chủ GCM sẽ chuyển thông điệp này đến thiết bị di động registration id củathiết bị

Google Cloud Messaging cho AndroidCác bước thực hiện

Đầu tiên thiết bị android gửivàđến sever GCM để đăng kýSau khi ĐK thành công xuất sắc sever GCM sẽ phát sinh mộtcho thiết bị androidSau khi nhậnthiết bị sẽ gửiđến serverServer sẽ lưuvào cơ sở dữ liệuBất cứ khi nào cóthông báo, server sẽ gửi một thông điệp tới sever GCM cùng vớicủathiết bịMáy chủ GCM sẽ chuyển thông điệp này đến thiết bị di độngcủathiết bị

Một số hình ảnh của ứng dụng cài đặt google cloud messaging cho android

Trước khi nhập thông tin và ĐK

*

*

*

Nhập thông tin tại trang web và click vào nút “Send“, dữ liệu sẽ được gửi đến ứng dụng android

*

Bước 1: Đăng ký vớiGoogle Cloud Messaging

Truy cập trangGoogle APIs Console pagevà tạo một project mới

Chọn Create project

Bước 2:TạoMySQL Database (Tham khảo bài viết hướng dẫn kết android với mysql)

Truy cập tranghttp://localhost/phpmyadmin và tạo một database tên gcm (Nếu localhost của bạn đang chạy trên port khác 80 thì bạn phảithêm portvào urltheo định dạnghttp://localhost:port/phpmyadmin)

Sau khi tạo xong database, bạn chọn gcm và thực thi đoạn script sau để tạo bảnggcm_web5_users

CREATE TABLE IF NOT EXISTS gcm_web5_users ( id int(11) NOT NULL AUTO_INCREMENT, gcm_regid text, name varchar(50) NOT NULL, email varchar(255) NOT NULL, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;Bước 3: Tạo PHP Project

Tạo thư mục têngcm_server_php trong thư mụchtdocs

Giải nén tập tingcm_server_php.jar vào thư mụcgcm_server_php

Lưu ý thay đổi thông tin kết nối đến MySQL và API Key tạiconfig.php

Chọn SDK Tools -> chọn Show PackageDetails -> chọnGoogle Cloud Messaging for Android Library -> chọn OK

Bước 5: TạoAndroid Project

5.1. Sử dụng Android Studio và tạo mới một project tên GCMDemo,tải và copy các file đặt vào thư mục drawable

5.2. Vàosdkextrasgooglegcmsamplesgcm-demo-clientlibs và copygcm.jar -> sau đópaste vào thư mụcapplibs

Kết quả sau khi paste thành công xuất sắc

5.3. Trong Android Studio -> mở file build.gradle (Module:app) và thêm vào đoạn code

compile files(“libs/gcm.jar”)

5.4. Tạo tập tinCommonUtilities.java

Chuột phải package -> chọn New -> chọn Java Class

Nội dung tập tinparam message message to be displayed. */ static void displayMessage(Context context, String message) { Intent intent = new Intent(DISPLAY_MESSAGE_ACTION); intent.putExtra(EXTRA_MESSAGE, message); context.sendBroadcast(intent); }}Lưu ý thay đổi các thông sau cho phù hợp với máy của bạn

param message message to be displayed. * / static void displayMessage ( Context context, String message ) { Intent intent = new Intent ( DISPLAY_MESSAGE_ACTION ) ; intent. putExtra ( EXTRA_MESSAGE, message ) ; context. sendBroadcast ( intent ) ; } } Lưu ý biến hóa những thông sau cho tương thích với máy của bạn

5.5. Tạo tập tinServerUtilities.java

import android.content.Context;import android.util.Log;import com.google.android.gcm.GCMRegistrar;import java.io.IOException;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Random;/** * Created by honamphoto.com */public class ServerUtilities { private static final int MAX_ATTEMPTS = 5; private static final int BACKOFF_MILLI_SECONDS = 2000; private static final Random random = new Random(); /** * Register this account/device pair within the server. * */ static void register(final Context context, String name, String email, final String regId) { Log.i(CommonUtilities.TAG, “registering device (regId = ” + regId + “)”); String serverUrl = CommonUtilities.SERVER_URL; Map params = new HashMap(); params.put(“regId”, regId); params.put(“name”, name); params.put(“email”, email); long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000); // Once GCM returns a registration id, we need to register on our server // As the server might be down, we will retry it a couple // times. for (int i = 1; i params = new HashMap(); params.put(“regId”, regId); try { post(serverUrl, params); GCMRegistrar.setRegisteredOnServer(context, false); String message = context.getString(R.string.server_unregistered); CommonUtilities.displayMessage(context, message); } catch (IOException e) { // At this point the device is unregistered from GCM, but still // registered in the server. // We could try to unregister again, but it is not necessary: // if the server tries to send a message to the device, it will get // a “NotRegistered” error message and should unregister the device. String message = context.getString(R.string.server_unregister_error, e.getMessage()); CommonUtilities.displayMessage(context, message); } } /** * Issue a POST request to the server. * *

throws IOException propagated from POST. */ private static void post(String endpoint, Map params) throws IOException { URL url; try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException(“invalid url: ” + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Entry param = iterator.next(); bodyBuilder.append(param.getKey()).append(“=”) .append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append(“&”); } } String body = bodyBuilder.toString(); Log.v(CommonUtilities.TAG, “Posting “” + body + “” to ” + url); byte bytes = body.getBytes(); HttpURLConnection conn = null; try { Log.e(“URL”, “> ” + url); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod(“POST”); conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded;charset=UTF-8”); // post the request OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); // handle the response int status = conn.getResponseCode(); if (status != 200) { throw new IOException(“Post failed with error code ” + status); } } finally { if (conn != null) { conn.disconnect(); } } }}5.6. Tạo tập tinGCMIntentService.java

import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.util.Log;import com.google.android.gcm.GCMBaseIntentService;/** * Created by honamphoto.com */public class GCMIntentService extends GCMBaseIntentService { private static final String TAG = “GCMIntentService”; public GCMIntentService() { super(CommonUtilities.SENDER_ID); } /** * Method called on device registered **/

Override protected void onRegistered(Context context, String registrationId) { Log.i(TAG, “Device registered: regId = ” + registrationId); CommonUtilities.displayMessage(context, “Your device registred with GCM”); Log.d(“NAME”, MainActivity.name); ServerUtilities.register(context, MainActivity.name, MainActivity.email, registrationId); } /** * Method called on device un registred * */

Xem thêm : Hình Thức Kế Toán Chứng Từ Ghi Sổ Là Gì, Những Cách Lập Chứng Từ Ghi SổOverride protected void onUnregistered(Context context, String registrationId) { Log.i(TAG, “Device unregistered”); CommonUtilities.displayMessage(context, getString(R.string.gcm_unregistered)); ServerUtilities.unregister(context, registrationId); } /** * Method called on Receiving a new message * */

Override protected void onMessage(Context context, Intent intent) { Log.i(TAG, “Received message”); String message = intent.getExtras().getString(“msg”); CommonUtilities.displayMessage(context, message); // notifies web5_user generateNotification(context, message); } /** * Method called on receiving a deleted message * */

Override protected void onDeletedMessages(Context context, int total) { Log.i(TAG, “Received deleted messages notification”); String message = getString(R.string.gcm_deleted, total); CommonUtilities.displayMessage(context, message); // notifies web5_user generateNotification(context, message); } /** * Method called on Error * */

Override public void onError(Context context, String errorId) { Log.i(TAG, “Received error: ” + errorId); CommonUtilities.displayMessage(context, getString(R.string.gcm_error, errorId)); }

Override protected boolean onRecoverableError(Context context, String errorId) { // log message Log.i(TAG, “Received recoverable error: ” + errorId); CommonUtilities.displayMessage(context, getString(R.string.gcm_recoverable_error, errorId)); return super.onRecoverableError(context, errorId); } /** * Issues a notification to inform the web5_user that server has sent a message. */ private static void generateNotification(Context context, String message) { long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); String title = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // set intent so it does not start a new activity PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Notification.Builder builder = new Notification.Builder(context); builder.setAutoCancel(false); builder.setContentTitle(title); builder.setContentText(message); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentIntent(pendingIntent); Notification notification = builder.getNotification(); notificationManager.notify(0, notification); }}5.7. Tạo tập tinAlertDialogManager.javaOverride protected void onUnregistered ( Context context, String registrationId ) { Log. i ( TAG, ” Device unregistered ” ) ; CommonUtilities. displayMessage ( context, getString ( R.string.gcm _unregistered ) ) ; ServerUtilities. unregister ( context, registrationId ) ; } / * * * Method called on Receiving a new message * * / Override protected void onMessage ( Context context, Intent intent ) { Log. i ( TAG, ” Received message ” ) ; String message = intent. getExtras ( ). getString ( ” msg ” ) ; CommonUtilities. displayMessage ( context, message ) ; / / notifies web5_user generateNotification ( context, message ) ; } / * * * Method called on receiving a deleted message * * / Override protected void onDeletedMessages ( Context context, int total ) { Log. i ( TAG, ” Received deleted messages notification ” ) ; String message = getString ( R.string.gcm _deleted, total ) ; CommonUtilities. displayMessage ( context, message ) ; / / notifies web5_user generateNotification ( context, message ) ; } / * * * Method called on Error * * / Override public void onError ( Context context, String errorId ) { Log. i ( TAG, ” Received error : ” + errorId ) ; CommonUtilities. displayMessage ( context, getString ( R.string.gcm _error, errorId ) ) ; } Override protected boolean onRecoverableError ( Context context, String errorId ) { / / log message Log. i ( TAG, ” Received recoverable error : ” + errorId ) ; CommonUtilities. displayMessage ( context, getString ( R.string.gcm _recoverable_error, errorId ) ) ; return super. onRecoverableError ( context, errorId ) ; } / * * * Issues a notification to inform the web5_user that server has sent a message. * / private static void generateNotification ( Context context, String message ) { long when = System. currentTimeMillis ( ) ; NotificationManager notificationManager = ( NotificationManager ) context. getSystemService ( Context. NOTIFICATION_SERVICE ) ; String title = context. getString ( R.string.app _name ) ; Intent notificationIntent = new Intent ( context, MainActivity. class ) ; notificationIntent. setFlags ( Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP ) ; / / set intent so it does not start a new activity PendingIntent pendingIntent = PendingIntent. getActivity ( context, 0, notificationIntent, 0 ) ; Notification. Builder builder = new Notification. Builder ( context ) ; builder. setAutoCancel ( false ) ; builder. setContentTitle ( title ) ; builder. setContentText ( message ) ; builder. setSmallIcon ( R.mipmap.ic _launcher ) ; builder. setContentIntent ( pendingIntent ) ; Notification notification = builder. getNotification ( ) ; notificationManager. notify ( 0, notification ) ; } } 5.7. Tạo tập tin

import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;/** * Created by honamphoto.com */public class AlertDialogManager { /** * Function to display simple Alert Dialog *

param context – application context *

param title – alert dialog title *

param message – alert message *

param status – success/failure (used to set icon) * – pass null if you don”t want icon * */ public void showAlertDialog(Context context, String title, String message, Boolean status) { AlertDialog alertDialog = new AlertDialog.Builder(context).create(); // Setting Dialog Title alertDialog.setTitle(title); // Setting Dialog Message alertDialog.setMessage(message); if(status != null) // Setting alert dialog icon alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); // Setting OK Button alertDialog.setButton(“OK”, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); // Showing Alert Message alertDialog.show(); }}5.8. Tạo tập tin ConnectionDetector.java

import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;/** * Created by honamphoto.com */public class ConnectionDetector { private Context _context; public ConnectionDetector(Context context){ this._context = context; } /** * Checking for all possible internet providers * **/ public boolean isConnectingToInternet(){ ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i 5.9. Tạo tập tin WakeLocker.java

import android.content.Context;import android.os.PowerManager;public abstract class WakeLocker { private static PowerManager.WakeLock wakeLock; public static void acquire(Context context) { if (wakeLock != null) wakeLock.release(); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, “WakeLock”); wakeLock.acquire(); } public static void release() { if (wakeLock != null) wakeLock.release(); wakeLock = null; }}5.10. Tạo activity tênRegisterActivity

Thiết kế layout

Viết giải quyết và xử lý

import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;public class RegisterActivity extends AppCompatActivity { AlertDialogManager alert = new AlertDialogManager(); // Internet detector ConnectionDetector cd; // UI elements EditText txtName; EditText txtEmail; // Register button Button btnRegister;

Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); cd = new ConnectionDetector(getApplicationContext()); // Check if Internet present if (!cd.isConnectingToInternet()) { // Internet Connection is not present alert.showAlertDialog(RegisterActivity.this, “Internet Connection Error”, “Please connect to working Internet connection”, false); // stop executing code by return return; } // Check if GCM configuration is set if (CommonUtilities.SERVER_URL == null || CommonUtilities.SENDER_ID == null || CommonUtilities.SERVER_URL.length() == 0 || CommonUtilities.SENDER_ID.length() == 0) { // GCM sernder id / server url is missing alert.showAlertDialog(RegisterActivity.this, “Configuration Error!”, “Please set your Server URL and GCM Sender ID”, false); // stop executing code by return return; } txtName = (EditText) findViewById(R.id.txtName); txtEmail = (EditText) findViewById(R.id.txtEmail); btnRegister = (Button) findViewById(R.id.btnRegister); /* * Click event on Register button * */ btnRegister.setOnClickListener(new View.OnClickListener() {

Override public void onClick(View arg0) { // Read EditText dat String name = txtName.getText().toString(); String email = txtEmail.getText().toString(); // Check if web5_user filled the form if (name.trim().length() > 0 && email.trim().length() > 0) { // Launch Main Activity Intent i = new Intent(getApplicationContext(), MainActivity.class); // Registering web5_user on our server // Sending registraiton details to MainActivity i.putExtra(“name”, name); i.putExtra(“email”, email); startActivity(i); finish(); } else { // web5_user doen”t filled that data // ask him to fill the form alert.showAlertDialog(RegisterActivity.this, “Registration Error!”, “Please enter your details”, false); } } }); }}5.11. Thiết kế và viết xử lý cho MainActivity

Thiết kế layout

Viết giải quyết và xử lý

import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.AsyncTask;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import android.widget.Toast;import com.google.android.gcm.GCMRegistrar;import static android.gcmdemo.CommonUtilities.DISPLAY_MESSAGE_ACTION;import static android.gcmdemo.CommonUtilities.EXTRA_MESSAGE;import static android.gcmdemo.CommonUtilities.SENDER_ID;public class MainActivity extends AppCompatActivity { // label to display gcm messages TextView lblMessage; // Asyntask AsyncTask mRegisterTask; // Alert dialog manager AlertDialogManager alert = new AlertDialogManager(); // Connection detector ConnectionDetector cd; public static String name; public static String email;

Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// GCMRegistrar.unregister(getApplicationContext()); cd = new ConnectionDetector(getApplicationContext()); // Check if Internet present if (!cd.isConnectingToInternet()) { // Internet Connection is not present alert.showAlertDialog(MainActivity.this, “Internet Connection Error”, “Please connect to working Internet connection”, false); // stop executing code by return return; } // Getting name, email from intent Intent i = getIntent(); name = i.getStringExtra(“name”); email = i.getStringExtra(“email”); // Make sure the device has the proper dependencies. GCMRegistrar.checkDevice(this); // Make sure the manifest was properly set – comment out this line // while developing the app, then uncomment it when it”s ready. GCMRegistrar.checkManifest(this); lblMessage = (TextView) findViewById(R.id.lblMessage); registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION)); // Get GCM registration id final String regId = GCMRegistrar.getRegistrationId(this); // Check if regid already presents if (regId.equals(“”)) { // Registration is not present, register now with GCM GCMRegistrar.register(this, SENDER_ID); } else { // Device is already registered on GCM if (GCMRegistrar.isRegisteredOnServer(this)) { // Skips registration. Toast.makeText(getApplicationContext(), “Already registered with GCM”, Toast.LENGTH_LONG).show(); } else { // Try to register again, but not in the UI thread. // It”s also necessary to cancel the thread onDestroy(), // hence the use of AsyncTask instead of a raw thread. final Context context = this; mRegisterTask = new AsyncTask() {

Override protected Void doInBackground(Void… params) { // Register on our server // On server creates a new web5_user ServerUtilities.register(context, name, email, regId); return null; }

Override protected void onPostExecute(Void result) { mRegisterTask = null; } }; mRegisterTask.execute(null, null, null); } } } /** * Receiving push messages * */ private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

Override public void onReceive(Context context, Intent intent) { String newMessage = intent.getExtras().getString(EXTRA_MESSAGE); // Waking up mobile if it is sleeping WakeLocker.acquire(getApplicationContext()); /** * Take appropriate action on this message * depending upon your app requirement * For now i am just displaying it on the screen * */ // Showing received message lblMessage.append(newMessage + “n”); Toast.makeText(getApplicationContext(), “New Message: ” + newMessage, Toast.LENGTH_LONG).show(); // Releasing wake lock WakeLocker.release(); } };

Override protected void onDestroy() { if (mRegisterTask != null) { mRegisterTask.cancel(true); } try { unregisterReceiver(mHandleMessageReceiver); GCMRegistrar.onDestroy(this); } catch (Exception e) { Log.e(“UnRegister Receiver Error”, e.getMessage()); } super.onDestroy(); }}5.12.Nội dung tập tinAndroidManifest.xml

More on this topic

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Advertismentspot_img

Popular stories