Android GCM basic implementation

前端 未结 9 1903
鱼传尺愫
鱼传尺愫 2020-12-01 04:54

UPDATE: I fixed the problems in the code below so this makes a nice basic working example of how to use GCM


So, I\'m trying to im

相关标签:
9条回答
  • 2020-12-01 04:54

    I think you have some issues in the code.

    You should do the registration of your own broadcast receiver in the manifest file and that receiver will trigger the <service android:name=".GCMIntentService" />.

    Therefore you must do something like I write below.

    • The receiver must be declared like:

      <receiver
          android:name="your.package.name.YourBroadCastReceiver"
          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" />
          </intent-filter>
      

    • Your broadcast receiver that will start the service.

      public class YourBroadCastReceiver extends BroadcastReceiver {
      
      @Override
       public final void onReceive(Context context, Intent intent) {
           GCMIntentService .runIntentInService(context, intent);
           setResult(Activity.RESULT_OK, null, null);
       }  
      }
      

    I advice you to take a look at the official GCM documentation where you can find a good example.

    And... don't forget to enable the Google Cloud Messaging service in the main Google APIs Console page.

    Let me know if it helps!

    0 讨论(0)
  • 2020-12-01 04:58

    Not sure if this is related to your specific problem, however, I was experiencing the same issue. Everything was setup correctly, as far as I could tell, but the onRegistered() handler was never called.

    I had the standard onCreate(), onStart() and onDestroy() service methods as part of my GCMIntentService class. Once I removed them, the handler was called, as expected.

    My assumption is that by overriding these methods, I was by-passing needed code initialization.

    0 讨论(0)
  • 2020-12-01 05:02

    See below tutorial for GCM:-

    http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

    0 讨论(0)
  • 2020-12-01 05:08

    this is incorrect

    protected GCMIntentService( String senderId ) {         
    super(senderId);}
    

    As it states in the documentation. you must declare a PUBLIC, NO ARGUMENT constructor for GCMIntentService. Otherwise the GCMIntentService can't be instantiated properly by background intent services.

    public GCMIntentService(){
    super(senderID);}
    

    the senderID can be a hard coded constant string because it will no longer change with the new GCM. It's also very important you use the correct senderID. 24 hours is long enough for yours to be active so if my above solution doesn't work you are using the incorrect senderID. Everything else looks great.

    The senderID is in the URL of your web browser when you are browsing the Google API access page. Click on GCM and a 12 digit number will be present in the browser URL. That is the correct key to use. NOT your API key. That is used on the App server side.

    0 讨论(0)
  • 2020-12-01 05:14

    After reading ClouDeveloper's post I removed onCreate(), onDestroy(), and the other onXXX() callback methods. Also, my GCMIntentService.onRegistered(Context, regId) is called with the regId.

    Thank you ClouDeveloper!

    0 讨论(0)
  • 2020-12-01 05:16

    I also had this ANNOYing bug, til now. The Problem was, that i declared the intentservice inside another package... even if i declared the name in the android manifest and no classnotfound exception had been place... no error at all could be found... so i made sure the intentservice is in the root package.

    0 讨论(0)
提交回复
热议问题