在我的GCM客户端应用程序中收到通知时,我在日志中收到以下错误:
broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=package.name (has extras)
这会自动导致设备从GCM服务器取消注册:
Unregister application package.name for user 0
我已经阅读了类似的问题,但他们的解决方案似乎并不适用于我。
以下是app中使用的一些代码:
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package.name"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
<uses-permission android:name="android.permission.GET_ACCOUNTS"> </uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"> </uses-permission>
<uses-permission android:name="android.permission.VIBRATE"> </uses-permission>
<uses-permission android:name="android.permission.RECEIVE"> </uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<permission android:name="package.name.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="package.name.permission.C2D_MESSAGE"> </uses-permission>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application android:name="package.name.Globales"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="package.name.LoginActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:label="@string/app_name" >
</activity>
<activity
android:name="package.name.MainActivity"
android:label="app_name"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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.c2md.intent.RECEIVE"/>
<category android:name="package.name"/>
</intent-filter>
</receiver>
<service android:name="package.name.MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
<service android:name="package.name.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service android:name="package.name.RegistrationIntentService"
android:exported="false"></service>
</application>
</manifest>
GCM听众:
package package.name;
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.google.android.gms.gcm.GcmListenerService;
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, LoginActivity.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.mipmap.ic_launcher)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(001 /* ID of notification */, notificationBuilder.build());
}
}
php用于发送通知:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'KEY' );
//$registrationIds = array( $_GET['id'] );
$registrationIds=array('ID');
// prep the bundle
$msg = array
(
'message' => 'message',
'title' => 'title',
'subtitle' => 'subtitle',
'tickerText' => 'Ticker text',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'ic_launcher',
'smallIcon' => 'ic_launcher'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
设备注册就好了。 关于如何修复它的任何想法? 感谢。
答案 0 :(得分:0)
可能为时已晚,但您的Manifest中似乎有类型
动作android:name =“com.google.android。 c2md .intent.RECEIVE”
应为 c2dm
问候