Appemble Plugin - Push Notification using Google

What you will learn in this chapter
Server side - Google
Device side - Google
Integrate Push Notification plugin into your App
Download avm-google-push-notif.zip
Good Read - Appemble Plugin Push Notification using Apple, Push Notification using Amazon

Push Notification plugin enables your app to receive messages without actually opening the app. The messages appear on the phone’s home screen and can be viewed by expanding the status bar. They can also play a sound bite as well. The notification messages are sent by your server once your app user’s device registers for them.

Push Notifications allows an app to display a number or a ‘badge’ on the app’s icon. For example, the mail app will show a number of unread messages.

It is a good practice to ask permission from the user before sending push notification to the app.

How to enable Push Notifications?

For Push Notification to work you need to do the following.

  • Have a Server side Application

    • that provides APIs to register / deregister mobile devices.
    • Enables sending push notifications for each market place (Apple, Google, Amazon)
  • Integrate Appemble push notification plugin

    • Enables receiving push notification services for corresponding market place
    • Register/deregisters the device
    • To receive push notification for the corresponding market place

Server Side - Google

Prerequisites

Please read through the article GCM Getting Started and complete the following steps

Server Side implementation using Java

Please read through the article Writing the Server-side Application that describes the server side implementation using Java.

Server side implementation using PHP

You can download the sample project that implements the server side application supporting the required APIs from the git repository

git clone https://guest_appemble@bitbucket.org/appemble/appemble-google-messaging-tutorial-server.git

The project has following APIs

  • Register - See register.php
  • Deregister - See deregister.php
  • Send Push Notification to Google Market Place - See GCM.php

Configuration

  • The server uses mysql database to store the registered users. Create the database table for it using the script ddl.sql.

    CREATE TABLE IF NOT EXISTS `gcm_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 ;
    

    Note

    You can make changes to file db_connect.php to connect to a different database like Oracle or SQL server.

  • Configure the connection to database and to Google Cloud Messaging Servers (config.php)
    <?php
    /**
     * Database config variables
     */
    define("DB_HOST", "your server hosting the database.");
    define("DB_USER", "your database user name");
    define("DB_PASSWORD", "your password");
    define("DB_DATABASE", "your database name.");
    
    /*
     * Google API Key
     */
    define("GOOGLE_API_KEY", "your google API key"); // Place your Google API Key
    ?>
    

Device side - Google

You can go through the Google’s official documentation to know more about Google Cloud Messaging (GCM).

Prerequisites

Please read through the article GCM Getting Started and complete the following steps

Integrate Push Notification plugin into your App

Please follow the steps below to integrate the messaging plugin in your app.

  1. Add the messaging library in your project and optionally remove the appemble_android_library. todo add the picture on how to add the messaging library.

  2. Modify AndroidManifest.xml and add the following

    1. In the <manifest> tag add the following lines. Replace the PACKAGE with your application package name

      <!--  GCM Begin -->
      <!-- GCM connects to Google Services. -->
      <uses-permission android:name="android.permission.INTERNET" />
      
      <!-- GCM requires a Google account. -->
      <uses-permission android:name="android.permission.GET_ACCOUNTS" />
      
      <!-- Keeps the processor from sleeping when a message is received. -->
      <uses-permission android:name="android.permission.WAKE_LOCK" />
      
      <!--
       Creates a custom permission so only this app can receive its messages.
      
       NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
             where PACKAGE is your application's package name.
      -->
      <permission
          android:name="PACKAGE.permission.C2D_MESSAGE"
          android:protectionLevel="signature" />
      <uses-permission
          android:name="PACKAGE.permission.C2D_MESSAGE" />
      
      <!-- This app has permission to register and receive data message. -->
      <uses-permission
          android:name="com.google.android.c2dm.permission.RECEIVE" />
      
      <!--  GCM End -->
      
    2. Add the following lines, inside the <application> tag. Replace the PACKAGE with your application package name.

    <!-- GCM Begin -->
        <activity
            android:name="com.appemble.google.messaging.MessagingActivity"
            android:label="@string/app_name"/>
        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.
    
          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="PACKAGE" />
            </intent-filter>
        </receiver>
    
        <!--
          Application-specific subclass of GCMBaseIntentService that will
          handle received messages.
    
          By default, it must be named .GCMIntentService, unless the
          application uses a custom BroadcastReceiver that redefines its name.
        -->
        <service android:name="com.appemble.google.messaging.GCMIntentService"
            android:enabled="true"/>
    <!--  GCM End -->
    
  3. Modify the properties file res/values/string.xml and add the following

<string name="REGISTER_SERVER_URL">Your Server URL where devices can be register. For ex. http://www.example.com/register </string>
<string name="DEREGISTER_SERVER_URL">Your Server URL where devices can be deregistered. For ex. http://www.example.com/deregister</string>
<string name="SENDER_ID">YOUR PROJECT ID obtained from https://code.google.com/apis/console/</string>
  1. Register the device - Call the action com.appemble.messaging.REGISTER to register a device. It is recommended that user’s permission is taken before registering their device.
  2. Deregister a device. The action com.appemble.messaging.DEREGISTER is called to deregister a device.