0

私はAndroidが初めてです。サーバー(Java)からAndroidフォンにメッセージ(GCM)を送信しようとしています。サーバー コードをデバッグすると、メッセージが正常に Android フォンに送信されることがわかりました。しかし、電話でメッセージを受け取ることができませんでした。onMessage() にブレークポイントを設定しようとしましたが、使用できませんでした。

サーバーからのすべてのメッセージの後、私の電話は次のような警告ボックスのみを受け取ります:「プロジェクトは応答していません。閉じますか?」 警告ボックスには2つのボタンがあります:待機とOK。

誰でもこれについて私を助けてください。サーバーはメッセージを正常に送信しましたが、電話でメッセージを受信して​​いなかったのはなぜですか。私のクライアントとサーバーのコードは次のとおりです。

サーバ:

public void sendPushNotification(String theRegistrationId, String thePushMsg)
    {
        //Used to transmit messages to the Google Cloud Messaging service.
        Sender aGcmSender = new Sender(API_KEY); 

        //Constructing message which need to be transmitted to android device.      
        Message aMessage = new Message.Builder().addData("message", thePushMsg).build(); 
        try 
        {
            Result result = aGcmSender.send(aMessage, theRegistrationId, 1); 
        }
        catch (Exception theException) 
        {

        }
    }

クライアント: AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<permission android:name="com.test.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.test.app.permission.C2D_MESSAGE" />

<uses-permission android:name="android.permission.INTERNET" />

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="MyApplication">
    <uses-library android:name="com.google.android.maps" />

    <activity
        android:name=".view.welcome.WelcomeView"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <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" />
        <category android:name="com.test.app" />
      </intent-filter>
    </receiver>

    <service android:name="com.test.app.GCMIntentService" />

</application>

GCMIntentService:

package com.test.app;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService
{
    private static final String TAG = "===GCMIntentService===";
    private static String GCM_SENDER_ID = "697532398835"; //Project id in Google API Console

    public GCMIntentService() 
    {
        super(GCM_SENDER_ID);
        Log.d(TAG, "GCMIntentService init");
    }

    @Override
    protected void onRegistered(Context theContext, String theRegistrationId) 
    {
        Log.i(TAG, "Device registered: regId = " + theRegistrationId);
    }

    @Override
    protected void onUnregistered(Context theContext, String theArg) 
    {
        Log.i(TAG, "unregistered = "+theArg);
    }

    @Override
    protected void onMessage(Context theContext, Intent theIntent) 
    {
        Log.i(TAG, "new message= ");

        String aMessage = theIntent.getStringExtra("message");
        sendGCMIntent(theContext, aMessage);
    }

    private void sendGCMIntent(Context theContext, String theMessage) 
    {
        Intent aBroadcastIntent = new Intent();
        aBroadcastIntent.setAction("GCM_RECEIVED_ACTION");
        aBroadcastIntent.putExtra("gcm", theMessage);
        theContext.sendBroadcast(aBroadcastIntent);
    }

    @Override
    protected void onError(Context theContext, String theErrorId) 
    {
        Log.i(TAG, "Received error: " + theErrorId);
    }

    @Override
    protected boolean onRecoverableError(Context theContext, String theErrorId) 
    {
        return super.onRecoverableError(theContext, theErrorId);
    }
}

私の活動:

package com.test.app.view.welcome;

import java.net.MalformedURLException;
import java.net.URL;

import com.google.android.gcm.GCMRegistrar;

public class LoginView extends Activity implements Runnable {

    Typeface itsTypeFace;
    IntentFilter gcmFilter;
    String broadcastMessage = "No broadcast message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);

        gcmFilter = new IntentFilter();
        gcmFilter.addAction("GCM_RECEIVED_ACTION");         

    }

    private BroadcastReceiver gcmReceiver = new BroadcastReceiver() 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            broadcastMessage = intent.getExtras().getString("gcm");
            if (broadcastMessage != null) 
            {
                System.out.println(broadcastMessage);
            }
        }
    };

    /**
     * Method to avoid back key press for a higher api level 2.0 and above
     */
    @Override
    public void onBackPressed() {}

    /** If the user changes the orientation of his phone, the current activity
        is destroyed, and then re-created.  This means that our broadcast message
        will get wiped out during re-orientation.
        So, we save the broad cast message during an onSaveInstanceState()
        event, which is called prior to the destruction of the activity.
    */
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) 
    {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("BroadcastMessage", broadcastMessage);

    }

    /** When an activity is re-created, the os generates an onRestoreInstanceState()
        event, passing it a bundle that contains any values that you may have put
        in during onSaveInstanceState()
        We can use this mechanism to re-display our last broadcast message.
    */
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) 
    {
        super.onRestoreInstanceState(savedInstanceState);
        broadcastMessage = savedInstanceState.getString("BroadcastMessage");
    }

    /** If our activity is paused, it is important to UN-register any
        broadcast receivers.
    */
    @Override
    protected void onPause() 
    {
        unregisterReceiver(gcmReceiver);
        super.onPause();
    }

    /** When an activity is resumed, be sure to register any
        broadcast receivers with the appropriate intent
    */
    @Override
    protected void onResume() 
    {
        super.onResume();
        registerReceiver(gcmReceiver, gcmFilter);
    }

    /** 
     * The call to GCMRegistrar.onDestroy()
     */
    @Override
    public void onDestroy() 
    {
        GCMRegistrar.onDestroy(this);
        super.onDestroy();
    }
}

誰でもこの問題について私を助けてくれませんか。サーバーはメッセージを正常に送信しましたが、電話でメッセージを受信して​​いなかったのはなぜですか。

4

1 に答える 1

0

この問題の解決策を見つけました。アプリケーション (サーバー側) をデプロイすると、json-simple.jar に問題があることがわかりました。

jar をjson-simple-1.1.1.jarにアップグレードしたところ、うまくいきました!

また、NickT が示唆したように、canonicalid を次のように更新しました。

Sender aGcmSender = new Sender(API_KEY);     
//Constructing message which need to be transmitted to android device.      
Message aMessage = new Message.Builder().addData("message", thePushMsg).build(); 

Result result = aGcmSender.send(aMessage, REG_ID, 1); //Transmitting message to android device
if(result != null)
{
    if(result.getMessageId() != null)
    {
        String aCanonicalRegistrationId = result.getCanonicalRegistrationId();
        if(aCanonicalRegistrationId != null)
        {
            //Updated my database with aCanonicalRegistrationId (replaced REG_ID)
        }
    }
    else 
    {  
        String error = result.getErrorCodeName();
        if (error.equals(Constants.ERROR_NOT_REGISTERED)) {    
            // application has been removed from device - unregister database
        }
    }
   }
}

ありがとうございました。

于 2012-11-28T14:27:49.413 に答える