2

AndroidのアプリにGCMを実装しようとしています。サーバー側から文字列を「プッシュ」するとonMessageメソッドが呼び出されるため、サーバー側とクライアント側の設定は正しいようです。インテントからエクストラを読み取ることはできますが、通知またはトーストメッセージを使用しても機能しません。アプリを実行していても電話には何も表示されないため、コールバックから使用したコンテキストオブジェクトに問題があると思います。これがmanifest.xmlの重要な部分です。ここで、PACKAGEは基本パッケージです。

<receiver
   android:name="PACKAGE.gcm.GCMReceiver"
   android:enabled="true"
   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="PACKAGE" />
     </intent-filter>
</receiver>

<service
   android:name="PACKAGE.gcm.GCMIntentService"
   android:enabled="true" />
</application>

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

<uses-permission android:name="PACKAGE.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 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" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>

今onMessageメソッド:

@Override
    protected void onMessage(Context context, Intent intent)
    {
        if (DEBUG)
        {
            System.out.println("[GCMIntentService] Message Received! " + intent.getStringExtra("message"));
        }

        Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        // int icon = R.drawable.notification_icon;
        CharSequence tickerText = intent.getStringExtra("message");
        long when = System.currentTimeMillis();

        Notification notification = new Notification(0, tickerText, when);

        CharSequence contentTitle = "Some Notification";
        CharSequence contentText = tickerText + " some more text";

        notification.setLatestEventInfo(context, contentTitle, contentText, null);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);

    }

トーストと通知が機能していません。最初のアクティビティのonCreateでサービスと登録ルーチンを呼び出します。これはスプラッシュとして使用され、数秒後に閉じられます。それはそれと関係があるのでしょうか?

4

1 に答える 1

6

これはContext問題ではありません。

GCMレシーバーは、それ自体が別のスレッドで実行されるインテントサービスで実行されます。

トーストを表示するには、UIThread内から呼び出すだけです。あなたはそうすることができます:

Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable(){

    Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();

});

これでUIスレッドに投稿され、トーストが表示されます。

final(匿名クラスで実行するには、ローカル変数を作成する必要があります)

乾杯!

于 2012-08-28T17:05:41.583 に答える