GCM返回空消息类型 [英] GCM returns a null message type
问题描述
我创建一个使用GoogleCloudMessaging的应用程序。应用程序可以注册到GCM和存储其注册ID到数据库在我的服务器。我使用PHP,发送推送通知,但是当谷歌将其发送到我的设备,其目的服务认定其消息类型为空。我曾尝试在同一code在不同的应用程序,它运作良好。但这次没有。该应用程序可以从谷歌得到的消息,并表示用空文本通知处理。我公司提供的服务的意图和php codeS下面。谢谢您的回答。
send_message.php
< PHP
如果(使用isset($ _ GET [REGID])及和放大器;使用isset($ _ GET [信息])){
$ REGID = $ _GET [REGID];
$消息= $ _GET [信息];
include_once'./GCM.php;
$ GCM =新GCM();
$ registatoin_ids =阵列($ REGID);
$消息=阵列(价格=> $消息);
$结果= $ gcm-> send_notification($ registatoin_ids,$消息);
回声$结果;
}
?>
GCM.php
< PHP
一流的GCM {
//把你的code在这里
//构造
功能__construct(){
}
/ **
*发送推送通知
* /
公共职能send_notification($ registatoin_ids,$消息){
//包括配置
include_once'./config.php;
//设置POST变量
$ URL ='https://android.googleapis.com/gcm/send';
$领域=阵列(
registration_ids'=> $ registatoin_ids,
'数据'=> $消息,
);
$标题=阵列(
授权:键='。 GOOGLE_API_KEY,
内容类型:应用程序/ JSON
);
//打开连接
$ CH = curl_init();
//设置URL,POST数瓦尔,POST数据
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
//禁用SSL证书支持,即暂时
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,假);
curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));
//执行后
$结果= curl_exec($ CH);
如果($结果=== FALSE){
死亡('卷曲失败:curl_error($ CH)。);
}
//关闭连接
curl_close($ CH);
回声$结果;
}
}
?>
MyIntentService.java
公共类MyIntentService扩展IntentService {
公共静态最终诠释NOTIFICATION_ID = 1;
私人NotificationManager mNotificationManager;
NotificationCompat.Builder建设者;
公共MyIntentService(){
超级(MyIntentService);
}
@覆盖
保护无效onHandleIntent(意向意图){
Log.v(MainActivity.TAG,处理的意图。);
捆绑额外= intent.getExtras();
GoogleCloudMessaging GCM = GoogleCloudMessaging.getInstance(本);
//该getMessageType()的意图参数必须是您收到的意图
//你的BroadcastReceiver。
字符串为messageType = gcm.getMessageType(意向);
generateNotification(getApplicationContext(),extras.getString(代价));
Log.v(MainActivity.TAG,IntentService消息类型=+为messageType);
如果(!extras.isEmpty()){//具有unparcelling捆绑的效果
/ *
*根据消息类型过滤消息。因为它是可能的GCM将
*延伸并与新的信息类型以后,就忽略任何消息类型
*不感兴趣,或者说,你不承认。
* /
如果(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(为messageType)){
sendNotification的(发送错误:+ extras.toString());
}否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(为messageType)){
sendNotification的(在服务器上删除的邮件:+ extras.toString());
//如果这是一个普通的GCM消息,做了一些工作。
}否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(为messageType)){
//这个循环再presents服务做一些工作。
//的for(int i = 0;我小于5;我++){
// Log.i(TAG,工作......+(I + 1)
// +/ 5 @+ SystemClock.elapsedRealtime());
// 尝试 {
//视频下载(5000);
//}赶上(InterruptedException异常E){
//}
//}
Log.i(MainActivity.TAG,已完成的工作@+ SystemClock.elapsedRealtime());
收到消息的//邮政通知。
sendNotification的(收到:+ extras.toString());
generateNotification(getApplicationContext(),
收到:+ extras.getString(代价));
Log.i(MainActivity.TAG,收到:+ extras.toString());
}
}
//释放由WakefulBroadcastReceiver提供的唤醒锁。
WakefulBroadcastReceiver.completeWakefulIntent(意向);
}
总之,Log.v(MainActivity.TAG,IntentService消息类型=+为messageType);显示IntentService消息类型=空。我该如何解决这个问题呢?
公共类GcmBroadcastReceiver扩展WakefulBroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
//明确指定GcmIntentService会处理这个意图。
组件名补偿=新的组件名(context.getPackageName()
GcmIntentService.class.getName());
//启动服务,保持设备时推出清醒。
startWakefulService(上下文,(intent.setComponent(化合物)));
的setResult code(Activity.RESULT_OK);
}
}
检查你的AndroidManifest.xml! 和修复设置GCM如下图所示。 就我而言,我解决了这个空的问题。 祝你好运!
<许可
机器人:名=[我的包名称] .permission.C2D_MESSAGE
安卓的ProtectionLevel =签名/>
...
<使用-许可安卓名=[我的包名称] .permission.C2D_MESSAGE/>
...
<服务机器人:名称=[我的包名称] .GCMIntentService/>
...
<接收器
机器人:名=[我的包名称] .GcmBroadcastReceiver机器人:出口=真
机器人:权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
<类安卓名=[我的包名称]/>
&所述; /意图滤光器>
< /接收器>
I created an application that uses GoogleCloudMessaging. Application can register to the gcm and store its registration id to the database at my server. I am using php, for sending push notifications but when the google sends it to my device, the intent service finds its message type to be null. I have tried the same code in a different application and it worked well. but this time it doesn't. The application can get the message from google and handle it by showing a notification with an empty text. I provided the intent service and php codes below. Thanks for your answers.
send_message.php
<?php
if (isset($_GET["regId"]) && isset($_GET["message"])) {
$regId = $_GET["regId"];
$message = $_GET["message"];
include_once './GCM.php';
$gcm = new GCM();
$registatoin_ids = array($regId);
$message = array("price" => $message);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
}
?>
GCM.php
<?php
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
}
?>
MyIntentService.java
public class MyIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v(MainActivity.TAG, "Handling intent.");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
generateNotification(getApplicationContext(), extras.getString("price"));
Log.v(MainActivity.TAG, "IntentService messagetype= " + messageType);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM will be
* extended in the future with new message types, just ignore any message types
* not interested in, or that you don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
// for (int i = 0; i < 5; i++) {
// Log.i(TAG, "Working... " + (i + 1)
// + "/5 @ " + SystemClock.elapsedRealtime());
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// }
// }
Log.i(MainActivity.TAG, "Completed work @ " +SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " + extras.toString());
generateNotification(getApplicationContext(),
"Received:" + extras.getString("price"));
Log.i(MainActivity.TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
In short, "Log.v(MainActivity.TAG, "IntentService messagetype= " + messageType);" displays "IntentService messagetype= null". How can i solve this problem?
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Check you AndroidManifest.xml ! And Fix settings for GCM like below. In my case, I solved this "null" problem. Good luck!
<permission
android:name="[MY PACKAGE NAME].permission.C2D_MESSAGE"
android:protectionLevel="signature" />
...
<uses-permission android:name="[MY PACKAGE NAME].permission.C2D_MESSAGE" />
...
<service android:name="[MY PACKAGE NAME].GCMIntentService" />
...
<receiver
android:name="[MY PACKAGE NAME].GcmBroadcastReceiver" android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="[MY PACKAGE NAME]" />
</intent-filter>
</receiver>
这篇关于GCM返回空消息类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!