【发布时间】:2015-08-20 05:32:02
【问题描述】:
我正在尝试在我的 Android 应用程序中使用 GCM 实现推送通知。我创建了一个服务器来成功地将消息推送到设备,还创建了一个 api 来成功地将设备令牌存储在服务器中。基于sample project 在link 中提到的应用程序中创建了一个客户端。并且还在 php 中创建了一个函数来向 App 推送通知。当我在服务器中运行该功能时,我得到了成功的响应。这意味着消息是从 gcm 发送到移动设备的。但是通知没有显示,而是我在日志猫中得到以下信息。
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
为了捕获消息,我使用了示例应用程序中表示的相同侦听器服务(我在下面添加以供参考)。所以我想到了通知管理器中的问题。所以我将其注释掉并运行应用程序。但我再次得到相同的回复,没有任何通知。不知道是什么问题。
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);
}
}
但是每当从服务器接收到消息时,onMessageReceived 必须在上述侦听器中调用。在onMessageReceived 函数中有两个日志函数来显示消息和发送者。那也没有执行。这意味着函数本身没有执行。下面我添加了清单内容。
<?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>
现在我仍然无法调试问题所在。我做错了什么还是错过了什么?
【问题讨论】:
-
我刚刚偶然发现了同样的问题。但是,我没有收到警告
W/GcmNotification﹕ Failed to show notification: Missing icon。您能否还显示您发送给 gcm 的内容(您的 curl 请求) -
您是否在使用 Android API 21 的设备上进行测试? setColor 是在 21 中添加的,看起来有些库仍然使用 Notification,而不是 NotificationCompat。
-
尝试实施@rohitsakala 建议的更改,并从您的 SDK 管理器更新您的支持存储库、库和播放服务。希望这会有所帮助。
-
感谢@peshkira 的回复。 rohitsakala 解决方案解决了这个问题。在推送通知时我在服务器的 json 中添加图标后,我在设备中收到通知。
标签: android push-notification google-cloud-messaging