Reputation: 8549
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?
Upvotes: 12
Views: 9723
Reputation: 399
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"
}
}
Upvotes: 12
Reputation: 491
Though an old thread, I wanted to add on to this based on some similar issues that I'd faced.
The app has two states: active and inactive.
If the app is active, sending a notification
without an icon value (I suspect the key is still required) would still work. Works for me but the key is still in there.
My JSON object looks like this:
.., icon=, sound=default, title=Bruce...
From developer docs:
For an active Android app, notification payloads are passed to onMessageReceived() under the notification key in the data bundle.
When the app is inactive:
Notifications are delivered to the notification tray when the app is inactive.
And here is when I hit the Missing icon
issue. So a key and value pair is required if the app is inactive.
If the payload is a notification, ensure that it always has the icon
key/value pair so that it will work as expected regardless of whether the app is active or inactive.
Finally, there should be a clear distinction between what a notification
is and what a data
is, in the design of the application. I had been using notification
for all push messages for my application which I think is not the right way of doing it.
Upvotes: 0
Reputation: 1
I encounter the same problem
look at the info in logcat:
dalvikvm﹕ Could not find method android.app.Notification$Builder.setColor, referenced from method com.google.android.gms.gcm.zza.zzv
dalvikvm﹕ VFY: unable to resolve virtual method 173: Landroid/app/Notification$Builder;.setColor (I)Landroid/app/Notification$Builder;
and reference the API doc: http://developer.android.com/reference/android/app/Notification.Builder.html
search "setColor" you will find out it added in "API level 21", means this method only works above 5.0(Lollipop)
I tried the structure below test on 2 different OS device, 5.0 & 4.4 respectively, and only work on 5.0
{"to": "/topics/global","data": {"message": "<message>"},"notification": {"icon": "ic_stat_ic_notification","body": "Use Gradle Command","title": "Self test"}}
Note : i use the JSON test on OS 5.0, notification is shown, but onMessageReceived still not called
Upvotes: 0