GCM with custom broadcastreceiver

后端 未结 4 385
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 11:14

I am implementing gcm notifications in my application. Because I use my code to generate lot of application with different package names I cannot use standard mypackage.GCMI

4条回答
  •  不思量自难忘°
    2020-12-28 11:39

    To change package/class name for application/GCMIntentService/GCMBroadcastReceiver for Android GCM (using Eclipse ADT)

    Note: You are advised to verify that you can receive messages through GCM before making the following changes. To implement GCM in Android app using the application's default package name, see GCM: Getting Started.


    Package name

    To change package name of your app (e.g., new package name = com.example.newpackage),

    • In Package Explorer, right click the project → Android Tools → Rename Application Package.
      This updates the package name automatically and conveniently.
    • In AndroidManifest.xml, update the package name in permission and uses-permission for C2D_MESSAGE:

      
      
      
    • In AndroidManifest.xml, update the package name in category of the receiver:

        
          
              
              
      
                
          
      
      

    Name of GCMIntentService

    If your app's package name is com.example.newpackage, your GCMIntentService must be called com.example.newpackage.GCMIntentService. If not,

    • Create a new class that extends GCMBroadcastReceiver and override getGCMIntentServiceClassName():

      public class MyBroadcastReceiver extends GCMBroadcastReceiver
      {
          @Override
          protected String getGCMIntentServiceClassName(Context context)
          {
              return MyIntentService.class.getName(); // Don't hard-code like "com.example.oldpackage.MyIntentService", see http://stackoverflow.com/a/936696/1402846
          }
      }
      

      this is based on Google's documentation on GCMBroadcastReceiver:

      By default, the GCMBaseIntentService class belongs to the application main package and is named DEFAULT_INTENT_SERVICE_CLASS_NAME. To use a new class, the getGCMIntentServiceClassName(Context) must be overridden.

    • In AndroidManifest.xml, update the name of the receiver:

      
      
      
    • In AndroidManifest.xml, update the name of the service:

      
      

    Name of GCMBroadcastReceiver

    If your changed the package/class name of GCMBroadcastReceiver:

    • In AndroidManifest.xml, update the name of the receiver:

      
      
      

    Troubleshooting

    • Verify that in AndroidManifest.xml, the package name should appear at least 4 times:

      • In manifest:

      • In permission:

        
        
      • In uses-permission:

        
        
      • In category within intent-filter within receiver for the GCM broadcast receiver:

        
        
    • If you changed your package name, uninstall the old app on your device/emulator before testing.

    • If you changed your package name, notice that the registration ID (that you receive in onRegistered()) has changed.
    • If you received your registration ID in onRegistered(), you should see something like this in LogCat (tag GCMBroadcastReceiver):

      GCMBroadcastReceiver        onReceive: com.google.android.c2dm.intent.REGISTRATION
      GCMBroadcastReceiver        GCM IntentService class: com.example.oldpackage.MyIntentService
      

      Verify that the package/class name of the intent service is correct.

    • If you override getGCMIntentServiceClassName(Context) in your own GCMBroadcastReceiver, you should see something like this in LogCat (tag GCMRegistrar):

      GCMRegistrar        Setting the name of retry receiver class to com.example.oldpackage.MyBroadcastReceiver
      

      Verify that the package/class name of the broadcast receiver is correct.

    • When your server sends a message to GCM server, check the HTTP status code and HTTP response for errors.
    • And if you are desperate:
      • Check LogCat for errors.
      • Try restarting your device/emulator.
      • Try uninstalling your app on the device/emulator.
      • Try restarting Eclipse.
      • Try clean and re-build the project.
      • Try updating ADT (Pull down menu → Window → Android SDK Manager → Install packages).
      • Try updating Eclipse (Pull down menu → Help → Check for Updates).
      • Try restarting your computer.
      • Get a good sleep, or pray.

提交回复
热议问题