Android 如何模拟从Google云消息发送的消息?
我正在使用谷歌云消息API向Android设备发送推送通知。我已经实现了Android 如何模拟从Google云消息发送的消息?,android,google-app-engine,google-cloud-messaging,Android,Google App Engine,Google Cloud Messaging,我正在使用谷歌云消息API向Android设备发送推送通知。我已经实现了IntentService和相应的逻辑来处理来自GCM服务器的通知。问题是GCM有时需要长达15分钟的时间来发送消息,这使得调试非常痛苦 我一直在寻找如何模仿GCM,但没有找到任何适用于我的情况的解决方案我已经实现了第三方客户端服务器;问题在于等待GCM将消息实际发送到Android设备 Android设备上的入口点是IntentService,它有一个hook方法handleIntent(Intent)。一种可能是编写另一
IntentService
和相应的逻辑来处理来自GCM服务器的通知。问题是GCM有时需要长达15分钟的时间来发送消息,这使得调试非常痛苦
我一直在寻找如何模仿GCM,但没有找到任何适用于我的情况的解决方案我已经实现了第三方客户端服务器;问题在于等待GCM将消息实际发送到Android设备
Android设备上的入口点是IntentService
,它有一个hook方法handleIntent(Intent)
。一种可能是编写另一个程序,向系统发送“欺骗”意图,这样系统加载myIntentService
时,其行为、外观和感觉都像是真实的GCM意图。这样,我的应用程序可以即时接收消息
有没有人遇到过这个问题,或者有没有关于如何解决这个问题的技巧?如果你想模仿某件事,而你不知道怎么做,请使用以下方法
// Please note that the authorization header's KEY is actually "key=<your GCM API key>"
Authorization: key=<your GCM API key>
Content-Type: application/json
{
"registration_ids":["<Your device registration token from GCM>"],
"data": {
"message" : "your message here"
}
}
public class PushNotificationListenerService extends GcmListenerService {
private static final String TAG = "NotificationListener";
/**
* 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) {
// Pay attention to this line of code; this indicates
// that you could have ANY key-value pair passed in
// as long as it's capable of being serialized into a Bundle object
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
/**
* In some cases it may be useful to show a notification
* indicating to the user that a message was received.
*/
sendNotification(message);
//TODO any of your own logic to handle the notification
}
// [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_favorite_white_shadow_36dp)
.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());
}
}