Android 如何模拟从Google云消息发送的消息?

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)。一种可能是编写另一

我正在使用谷歌云消息API向Android设备发送推送通知。我已经实现了
IntentService
和相应的逻辑来处理来自GCM服务器的通知。问题是GCM有时需要长达15分钟的时间来发送消息,这使得调试非常痛苦

我一直在寻找如何模仿GCM,但没有找到任何适用于我的情况的解决方案我已经实现了第三方客户端服务器;问题在于等待GCM将消息实际发送到Android设备

Android设备上的入口点是
IntentService
,它有一个hook方法
handleIntent(Intent)
。一种可能是编写另一个程序,向系统发送“欺骗”意图,这样系统加载my
IntentService
时,其行为、外观和感觉都像是真实的GCM意图。这样,我的应用程序可以即时接收消息


有没有人遇到过这个问题,或者有没有关于如何解决这个问题的技巧?

如果你想模仿某件事,而你不知道怎么做,请使用以下方法

  • 使用需要模拟的依赖项(intentservice)创建一个适配器类(CAdpapter)
  • 制作一些调用依赖性的公共方法
  • 创建一个接口(IAdapater),并确保适配器类实现该接口(只需将您在步骤2中创建的方法放在该接口中)
  • 确保需要与依赖性对话的类(如intentservice所述)不会直接这样做,它们与IAdapter实例对话
  • 编写一个实现IAdapter(MockAdapter)的模拟类。如果您不喜欢这样,请使用模拟框架
  • 类现在可以使用adapater与intentservice对话,也可以与Mock对话。一个解决方案是创建您自己的类,该类与您需要模拟的依赖性进行对话

    嘲弄可能很难。模拟需要实现与普通类相同的接口。然而,如果这个类有一个巨大的接口或者根本没有接口,那么它可能是一个问题(这些只是示例)。编写自己的类来调用需要模拟的类可以是一个解决方案

    我用来伪造通知

    您将需要标题:

    // 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());
        }
    }