簡體   English   中英

Android Cloud到設備的消息傳遞

[英]Android Cloud to Device Messaging

正在嘗試使用Android Cloud進行設備消息傳遞。 在注冊應用程序時遇到問題。 收到錯誤com.google.process.gapps asyncFetch:沒有用戶名。 我已經在下面包含了我的代碼

Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.putExtra("app",PendingIntent.getBroadcast(this, 0, new Intent(), 0));
intent.putExtra("sender", "xxx@gmail.com");
startService(intent);

我的注冊/接收人:

package com.mpest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyC2dmReceiver extends BroadcastReceiver{

 private static String KEY = "c2dmPref";
    private static String REGISTRATION_KEY = "registrationKey";

    private Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
            Log.i("aaa", "jnh");
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(context, intent);
        } 
        }
        private void handleRegistration(Context context, Intent intent) {
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
            Log.d("c2dm", "registration failed");
            String error = intent.getStringExtra("error");
            if(error.equals("SERVICE_NOT_AVAILABLE")){
                Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
            }else if(error.equals("ACCOUNT_MISSING")){
                Log.d("c2dm", "ACCOUNT_MISSING");
            }else if(error.equals("AUTHENTICATION_FAILED")){
                Log.d("c2dm", "AUTHENTICATION_FAILED");
            }else if(error.equals("TOO_MANY_REGISTRATIONS")){
                Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
            }else if(error.equals("INVALID_SENDER")){
                Log.d("c2dm", "INVALID_SENDER");
            }else if(error.equals("PHONE_REGISTRATION_ERROR")){
                Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
            }
            }
            else if (registration != null) {
            Log.d("c2dm", registration);
            //Editor editor =  context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
                // editor.putString(REGISTRATION_KEY, registration);
            //editor.commit();
           // Send the registration ID to the 3rd party site that is sending the messages.
           // This should be done in a separate thread.
           // When done, remember that all registration is done.
        }
    }

        private void handleMessage(Context context, Intent intent)
    {
            String mywish= intent.getStringExtra("wishes");    
              Toast.makeText(context,"my wishes : "+mywish,1).show(); 

        //Do whatever you want with the message
    }
}

我的表現

            <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> 
  <receiver android:name=".C2DMReceiver" 
  android:permission="com.google.android.c2dm.permission.SEND">
      <!-- Receive the actual message -->
      <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
          <category android:name="com.mpest.MyC2dmReceiver" />
      </intent-filter>
      <!-- Receive the registration id -->
      <intent-filter>
          <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
          <category android:name="com.mpest.MyC2dmReceiver" />
      </intent-filter>
  </receiver>

我猜您需要在模擬器上擁有一個有效的Google帳戶。 :)

在清單中添加一些權限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<permission android:name="com.mpest.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.mpest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM