Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.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
Php 未在GCM中的Android设备上接收消息_Php_Android_Google Cloud Messaging - Fatal编程技术网

Php 未在GCM中的Android设备上接收消息

Php 未在GCM中的Android设备上接收消息,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,我正在从事一个项目,该项目将GCM作为其主要功能 我遵循这一点来实现GCM客户机和GCM服务器 在xampp服务器中运行html文件时,我在屏幕上获得以下类型的输出: {"multicast_id":7075430686045407371, "success":1, "failure":0, "canonical_ids":0, "results":[ {"message_id":"0:1394625413704378%93abcc37f9fd7ecd"} ]

我正在从事一个项目,该项目将GCM作为其主要功能

我遵循这一点来实现GCM客户机和GCM服务器

在xampp服务器中运行html文件时,我在屏幕上获得以下类型的输出:

{"multicast_id":7075430686045407371,
  "success":1,
  "failure":0,
  "canonical_ids":0,
  "results":[
        {"message_id":"0:1394625413704378%93abcc37f9fd7ecd"}
  ]
}
在这里,我没有在我的Android设备上获得任何类型的消息或通知

我对php不太了解,所以我无法理解到底是怎么回事


我的php代码有问题吗?或者我的Android代码有问题吗?

确保推送通知有三个方面可以让它工作

服务器端:项目Id(可从谷歌云获取)


客户端:API密钥(也来自google cloud)和注册号(您的设备ID)

首先,您是否尝试过PHP脚本以查看与google服务器的连接是否成功?试着回应你的回答,看看这方面是否一切顺利

其次,您需要构建
GCMBroadcastReceiver'java
客户端以接收消息:

package myApp.packageName;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.games.Notifications;
import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * Handling of GCM messages.
 */
public class GcmBroadcastReceiver extends BroadcastReceiver {
    static final String TAG = "GCMDemo";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Context ctx;
    @Override
    public void onReceive(Context context, Intent intent)
    {

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        ctx = context;
        String messageType = gcm.getMessageType(intent);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
        {
            sendNotification("MyNotificationTitle", "Send error: " + intent.getExtras().toString());
        }
        else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
        {
            sendNotification("MyNotificationTitle", "Deleted messages on server: " + intent.getExtras().toString());
        }
        else
        {
            String payload = intent.getStringExtra("payload");
            String type = intent.getStringExtra("type");



            sendNotification("Received: " + intent.getExtras().toString());
        }

        setResultCode(Activity.RESULT_OK);

    }

    // Put the GCM message into a notification and post it.
    private void sendNotification(String title, String msg) {

        mNotificationManager = (NotificationManager)
                ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
                new Intent(ctx, myActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
        .setSmallIcon(R.drawable.icon)
        .setContentTitle(title)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg)
        .setDefaults(Notification.DEFAULT_SOUND);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

onRecieve()
方法中,尝试将消息打印到日志中,任何消息,只是为了查看事件是否正确触发。

您在PHP的config.xml文件中添加了api密钥。是的……我已将密钥替换为Mine。您得到的响应意味着Google接受了您的消息,因此您不必担心服务器代码。你能发布你的客户代码和清单吗?你的问题可能就在那里。您在LogCat中是否有任何错误?是的,我已经获得了所有必需的内容,但是我无法在设备上获得消息我已经从GCM服务器获得了对我的php脚本的响应{“multicast_id”:8626675218964447268,“success”:1,“failure”:0,“canonical_id”:0,“results”:[{“message_id:”0:1394690855107399%b06e6126f9fd7ecd}]这意味着什么?这意味着你已经成功地发送了你的信息。现在你只需要在客户端接收它们。