2013-12-16 49 views
0

我查看了几个教程,但仍无法在我的应用中完成此部分。 对不起,我很新,所以请不要低估我。Android Google云消息传递 - 无法完成

我就在我的表现得到了一切,在MainActivity我

public void registerService(){ 

    Intent registrationIntent=new Intent ("com.google.android.c2dm.intent.REGISTER"); 
    registrationIntent.putExtra("app",PendingIntent.getBroadcast(mContext,0, new Intent(), 0)); 
    registrationIntent.putExtra("sender", stringWithSenderID); 
    startService(registrationIntent); 
} 

GCMReceiver:

public class GCMReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     try{ 
      String action=intent.getAction(); 
      if (action.equals("com.google.android.c2dm.intent.REGISTRATION")){ 
       String registrationid=intent.getStringExtra("registration_id"); 
       Log.d("MyLog","registrationid ="+registrationid); 
       String error= intent.getStringExtra("error"); 
       String unregistered=intent.getStringExtra("unregistered"); 

      } 
      else if (action.equals("com.google.android.c2dm.intent.RECEIVE")){ 
       String data1=intent.getStringExtra("data1"); 
       String data2=intent.getStringExtra("data2"); 

      } 
     } finally{ 

     } 

    } 

} 

所以我得到了我的registration_id登录正确的,我该怎么做?我需要以某种方式将它发送给服务器吗?

+0

是,到您的服务器,从那里您将发送云命令。 –

+0

[如何从Android应用程序调用服务器应用程序servlet注册设备时,实施GCM]可能重复(http://stackoverflow.com/questions/18551266/how-to-call-server-app-servlet-from-android- – Eran

回答

0

我认为它可以帮助你:

public String Sender_id = "your_google_api_sender_id"; 
try { 
      GCMRegistrar.checkDevice(this); 
      GCMRegistrar.checkManifest(this); 
      String regId = GCMRegistrar.getRegistrationId(this); 

      if (regId.equals("")) { 
       GCMRegistrar.register(this, Sender_id); 
      } else { 
      } 

     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 

     } 

而在GCM Srevice我们要这样写:

public class GCMIntentService extends GCMBaseIntentService { 



    @Override 
    protected void onError(Context context, String error) { 
     // TODO Auto-generated method stub 
     if (error != null) { 
      // optionally retry using exponential back-off 
      // (see Advanced Topics) 
      Toast.makeText(getBaseContext(), "" + error, Toast.LENGTH_SHORT) 
        .show(); 
     } 

    } 

    public GCMIntentService() { 
     super("your_snde_id"); 
     // SENDER_ID is my project id into google account url 
     // TODO Auto-generated constructor stub 
    } 

    public GCMIntentService(String senderId) { 
     super(senderId); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     String message = intent.getStringExtra("message"); 
     // TODO Auto-generated method stub 
     //createNotification(context, message); 

    } 

    @Override 
    protected void onRegistered(Context context, String regId) { 
     // TODO Auto-generated method stub 
     // you need to handle what you have to do after registration 

    } 

    @Override 
    protected void onUnregistered(Context context, String error) { 
     // TODO Auto-generated method stub 

    } 





} 

和清单文件是这样的:

<permission 
     android:name="your.package.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="your.package.permission.C2D_MESSAGE" /> 
    <!-- App receives GCM messages. --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      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" /> 
       <action android:name="com.google.android.c2dm.intent.UNREGISTER" /> 

       <category android:name="com.activelifeapps.android.alAlleghany" /> 
      </intent-filter> 
     </receiver> 

     <service android:name="your.package.GCMIntentService" /> 
+0

方法createNotification(上下文,字符串)是未定义的类型GCMIntentService – user2976267

+0

这是我自己的方法来显示通知消息接收 –

+0

行..所以..我怎么做到这一点消息作为推送通知?我怎么能做出这样的消息,与我得到的其他推送通知一起使用? – user2976267

相关问题