大家好,我一直在实现gcm,但问题是它没有收到通知,我已经尝试过从我的项目Web服务器发送gcm消息并尝试在android上实现它但仍然没有用,它正在生成令牌ID但它没有收到任何消息,以下是我用于实现gcm
的类GcmServiceListner.class
public class GcmServiceListner extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
private PreferencesHandler ph;
@Override
public void onMessageSent(String msgId) {
super.onMessageSent(msgId);
Log.e("Anas", "MESSAGE SENT");
}
@Override
public void onMessageReceived(String from, Bundle data) {
try {
ph = new PreferencesHandler(this);
int notiNum = ph.getChatNotificationNumberName();
notiNum++;
ph.setChatNotificationNumberName(notiNum);
String message = data.getString("data");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* 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);
} catch (Exception e) {
e.printStackTrace();
}
// [END_EXCLUDE]
}
// [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, MainActivity.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_media_pause)
.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());
}
}
正如您所看到的,我已经覆盖了 onMessageSend 的方法,当我实现从应用程序内部发送消息时,它会在几分钟后调用此方法时发送消息通过gcm但仍然onMessageReceived方法不起作用,以下是我从应用程序调用send函数时的代码
gcm = GoogleCloudMessaging.getInstance(this);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
Log.e(TAG,"upstream");
try {
Bundle data = new Bundle();
data.putString("data", "Hello World");
String id = SENDING_ID;
gcm.send(to+"@gcm.googleapis.com", id, data);
msg = "Sent message";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Log.e(TAG, msg + " msg");
}
}.execute();
“to”over是我必须发送邮件的令牌ID,“SENDING_ID”是从Google服务器生成的ID
现在以下是我的web服务器的php代码,我正在实现gcm发送代码
define( 'API_ACCESS_KEY', API_KEY );
$fields = array(
'to' => TOKEN_ID,
'data' => "TEST MESSAGE",
);
$headers = array(
'Authorization: key='.API_KEY,
'Content-Type: application/json'
);
echo json_encode($fields);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL,"https://gcm-http.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_POSTFIELDS, json_encode($fields));
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
echo "ERROR IN CURL";
}else{
echo "ERROR FREE CURL";
}
请帮我解决我现在要做的事情: - )
编辑清单代码
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"></uses-permission>
<permission
android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<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" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my.package" />
</intent-filter>
</receiver>
<service
android:name="my.package.utility.GcmServiceListner"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="my.package.utility.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
</application>
答案 0 :(得分:0)
这是我的两个建议: 1.如果您使用物理手机测试打开的Chrome浏览器并将其发布在网址栏chrome:// inspect /#devices(这将显示您的设备是否已连接到您的PC端口)。 / p>
2.在上游的客户端,您应该更改代码
gcm.send(to+"@gcm.googleapis.com", id, data);
,因为: id是一个唯一的msg id,每次发送一个msg时都应该是唯一的(否则GCM可能会忽略它,并且它不会被发送到你的服务器)
最后你的服务器期待一个令牌,因此你应该有一个代码,当服务器收到一个消息时它会提取它(你的服务器现在没有提取任何它可以在上游或下游)
现在,如果你想进行基本的测试,你可以通过在TOKEN_ID
中手动复制和粘贴客户端的令牌(你的手机)以及你的api密钥来对你进行硬编码。 / p>