【问题标题】:Issue on receiving push notification on the GCM Client在 GCM 客户端上接收推送通知的问题
【发布时间】:2015-08-20 05:32:02
【问题描述】:

我正在尝试在我的 Android 应用程序中使用 GCM 实现推送通知。我创建了一个服务器来成功地将消息推送到设备,还创建了一个 api 来成功地将设备令牌存储在服务器中。基于sample projectlink 中提到的应用程序中创建了一个客户端。并且还在 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


【解决方案1】:

根据这个link,您应该有图标的键/值对。

检查通知负载的图标参数。根据需要指定。

因此,您必须在 JSON 中包含一个图标。

一个例子可能是

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

myicon 是应用程序中drawable 文件夹中名为myicon.png 的图像。

onMessageReceived 在收到带有data 有效负载的消息时被调用。当我们将上述 json 中的 notification 更改为 data(如下所示)时,将调用 onMessageReceived 函数。

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

【讨论】:

  • 谢谢@rohitsakala。你解决了我一半的问题。在我像上面那样更改为json 后,我收到了通知,但是在收到通知时没有调用函数onMessageReceived。你知道为什么吗?如果我得到上述问题的答案意味着我会接受它作为答案..
  • @Mahendran 您必须将通知更改为上述 json 中的数据才能调用 onMessageReceived !示例:- { "to": "ci4ZzC4BW....", "data": { "title": "Testing", "body": "Success", "icon": "@drawable/myicon" } } 和然后你可以通过通知生成器构建你的通知!
  • 一直在寻找,但这个答案恰到好处!是的,每个通知都需要有一个图标!非常感谢!
【解决方案2】:

我遇到了同样的问题

查看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;

并参考 API 文档: http://developer.android.com/reference/android/app/Notification.Builder.html

搜索“setColor”你会发现它是在“API level 21”中添加的,意味着这个方法只适用于5.0以上(棒棒糖)

我在 5.0 和 4.4 两个不同的操作系统设备上分别测试了下面的结构,并且只在 5.0 上工作

{"to": "/topics/global","data": {"message": "<message>"},"notification": {"icon": "ic_stat_ic_notification","body": "Use Gradle Command","title": "Self test"}}

注意: 我在 OS 5.0 上使用 JSON 测试,显示通知,但仍然没有调用 onMessageReceived

【讨论】:

  • 颜色在数据负载中不是特定的,所以考虑到它是可选的,这应该不会导致问题?我正在努力解决同样的问题,但需要支持 Jellybean 和更高版本
【解决方案3】:

虽然是一个旧线程,但我想根据我遇到的一些类似问题对此进行补充。

应用有两种状态:activeinactive

如果应用程序处于活动状态,发送没有图标notification(我怀疑仍然需要密钥)仍然可以工作。对我有用,但钥匙还在里面。

我的 JSON 对象如下所示:

.., icon=, sound=default, title=Bruce...

来自开发者文档:

对于一个活跃的 Android 应用,通知负载被传递到 onMessageReceived() 在数据包中的通知键下。

当应用处于非活动状态时:

当应用程序启动时,通知会发送到通知托盘 不活动。

这是我遇到Missing icon 问题的时候。因此,如果应用处于非活动状态,则需要键值对。

如果负载是通知,请确保它始终具有 icon 键/值对,以便无论应用程序处于活动还是非活动状态,它都能按预期工作。

See here for details

最后,在应用程序的设计中应该明确区分notificationdata。我一直使用notification 来处理我的应用程序的所有推送消息,我认为这不是正确的做法。

【讨论】:

    猜你喜欢
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多