Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在GCM客户端上接收推送通知时出现的问题_Android_Push Notification_Google Cloud Messaging - Fatal编程技术网

Android 在GCM客户端上接收推送通知时出现的问题

Android 在GCM客户端上接收推送通知时出现的问题,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我正在尝试在Android应用程序中使用GCM实现推送通知。我创建了一个服务器,成功地将消息推送到设备,还创建了一个api,成功地将设备令牌存储在服务器中。基于在中提到的应用程序中创建了客户端。并在php中创建了一个函数,用于将通知推送到应用程序。当我在服务器上运行该函数时,得到的响应是成功。这意味着信息从gcm发送到移动设备。但是没有显示通知,而是在日志中显示以下内容 06-05 14:58:59.172 23693-28001/com.greenboards.base I/dalvikvm

我正在尝试在Android应用程序中使用GCM实现推送通知。我创建了一个服务器,成功地将消息推送到设备,还创建了一个api,成功地将设备令牌存储在服务器中。基于在中提到的应用程序中创建了客户端。并在php中创建了一个函数,用于将通知推送到应用程序。当我在服务器上运行该函数时,得到的响应是成功。这意味着信息从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>

现在我仍然无法调试问题所在。我做错了什么还是遗漏了什么?

根据这一点,你应该有一对键/值图标

检查通知有效负载的图标参数。它是按要求指定的

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

一个例子可能是

{
    "to":   
"ci4ZzC4BW....",  
    "notification": {  
        "title": "Testing",  
        "body": "Success",  
        "icon": "@drawable/myicon"  
    }  
}
myicon
是应用程序中
drawable
文件夹中名为
myicon.png
的图像

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

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

我也遇到同样的问题

查看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文档:

搜索“setColor”,您会发现它添加在“API级别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"}}
{“to”:“/topics/global”,“data”:{“message”:“}”,通知:{“icon”:“ic_stat_ic_notification”,“body”:“Use Gradle Command”,“title”:“Self test”}
注:
我在OS 5.0上使用JSON测试,显示了通知,但onMessageReceived仍然没有调用

,虽然是一个旧线程,但我想基于我所面临的一些类似问题来增加它

该应用程序有两种状态:活动非活动

如果应用程序处于活动状态,则发送不带图标的
通知
值(我怀疑仍然需要钥匙)仍然有效。对我有用,但钥匙还在里面

我的JSON对象如下所示:

.., icon=, sound=default, title=Bruce...
从开发者文档:

对于活动的Android应用程序,通知有效载荷传递给 数据包中通知键下的onMessageReceived()

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

当应用程序启动时,通知会传递到通知托盘 不活跃

下面是我点击
缺失图标时出现的问题。因此,如果应用程序处于非活动状态,则需要密钥和值对

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


最后,在应用程序的设计中,应该明确区分什么是
通知
,什么是
数据
。我一直在为我的应用程序使用
通知
,我认为这不是正确的方法。

我刚刚偶然发现了同样的问题。但是,我没有收到警告
W/GcmNotification﹕ 无法显示通知:缺少图标
。您是否也可以展示您正在发送给gcm的内容(您的curl请求),您是否正在使用Android API 21的设备上进行测试?setColor是在21中添加的,看起来有些库仍然使用通知,而不是NotificationCompat。请尝试实施@rohitsakala建议的更改,并从SDK管理器更新您的支持存储库、库和播放服务。希望这能有所帮助。谢谢@peshkira的回复。rohitsakala解决方案解决了这个问题。在推送通知时,我在服务器的json中添加了图标后,我在设备中收到了通知。谢谢@rohitsakala。你解决了我问题的一半。在我像上面那样更改为
json
之后,我收到了通知,但是在收到通知时没有调用函数
onMessageReceived
。你知道为什么吗?如果我得到上述问题的答案,意味着我将接受它作为答案。@Mahendran您必须将通知更改为上述json中的数据,才能调用onMessageReceived!示例:-{“to”:“ci4ZzC4BW…”,“data”:{“title”:“Testing”,“body”:“Success”,“icon”:“@drawable/myicon”}}然后您可以通过通知生成器生成通知!我已经到处找了,但这个答案一针见血!是的,每个通知都需要有一个图标!非常感谢!颜色在数据有效负载中不是特定的,因此如果它是可选的,那么