Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue on receiving push notification on the GCM Client

I'm trying to implement push notification using GCM in my Android Application. I created a server to push messages to devices successfully and also created a api to store the device token in the server successfully. Created a client in application mentioned in the link based on the sample project. And also created a function in php to push notification to the App. When I run that function in the server I'm getting response as success. That means the message is sent from gcm to mobile. But the notification is not shown instead I'm getting the following in the log cat.

06-05 14:58:59.172  23693-28001/com.greenboards.base I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setColor, referenced from method com.google.android.gms.gcm.zza.zzv
06-05 14:58:59.172  23693-28001/com.greenboards.base W/dalvikvm﹕ VFY: unable to resolve virtual method 184: Landroid/app/Notification$Builder;.setColor (I)Landroid/app/Notification$Builder;
06-05 14:58:59.173  23693-28001/com.greenboards.base D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0068
06-05 14:58:59.175  23693-28001/com.greenboards.base W/GcmNotification﹕ Failed to show notification: Missing icon

To capture the message I'm using the same listener service which denoted in the example application(I added below for the reference). So I thought the issue in the notification manager. So I commented it out and run the application. But again I'm getting the same response without any notification. I don't know what is the issue.

package com.greenboards.base;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.greenboards.base.R;

import com.google.android.gms.gcm.GcmListenerService;
import com.greenboards.base.SplashScreen;

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        sendNotification(message);
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param message GCM message received.
     */
    private void sendNotification(String message) {
        /*Intent intent = new Intent(this, SplashScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 *//* Request code *//*, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("GCM Message")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 *//* ID of notification *//*, notificationBuilder.build());*/
        Log.v("notification message",message);
    }
}

But whenever the message received from the server the onMessageReceived must be called in the above listener. In the onMessageReceived function there is two logging function to show message and the sender. That is also not executing. That means the function itself is not executing. Below i added the manifest content.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.greenboards.base"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <!-- Application Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.greenboards.base.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.greenboards.base.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- Application Configuration and Activities -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- [START gcm_receiver] -->
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.greenboards.base" />
            </intent-filter>
        </receiver>
        <!-- [END gcm_receiver] -->

        <!-- [START gcm_listener] -->
        <service
            android:name="com.greenboards.base.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <!-- [END gcm_listener] -->
        <!-- [START instanceId_listener] -->
        <service
            android:name="com.greenboards.base.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <!-- [END instanceId_listener] -->
        <service
            android:name="com.greenboards.base.RegistrationIntentService"
            android:exported="false">
        </service>

    </application>
</manifest>

Still now I can't able to debug what is the issue. Am I doing anything wrong or am I am missing anything?

like image 738
Mahendran Sakkarai Avatar asked Jun 05 '15 09:06

Mahendran Sakkarai


1 Answers

According to this link you should have key/value pair of icon.

Check icon parameter of notification payload. It is specified as required.

So, you must include an icon in the JSON.

An example might be

{
    "to":   
"ci4ZzC4BW....",  
    "notification": {  
        "title": "Testing",  
        "body": "Success",  
        "icon": "@drawable/myicon"  
    }  
}

myicon is the image named myicon.png in drawable folder in the application.

onMessageReceived is called when a message with data payload is received. When we change notification to data(like below) in the above json the onMessageReceived function will be called.

{
    "to":   
"ci4ZzC4BW....",  
    "data": {  
        "title": "Testing",  
        "body": "Success",  
        "icon": "@drawable/myicon"  
    }  
}
like image 62
rohitsakala Avatar answered Sep 29 '22 11:09

rohitsakala