2

找不到此行为的解决方案:当从 GCM 接收消息时,清单中的广播接收器会正确处理它(创建通知),但如果应用程序在屏幕上,我希望它做另一件事(显示敬酒并取消通知)。

这是清单上的代码:

    <receiver android:name="com.google.android.gcm.GCMBroadcastReceiverPlus"
              android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
    <!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- Category -->
<category android:name="org.example.hello" />
</intent-filter>

这是适用于 JB 的代码,但不在下面:

    public BroadcastReceiver registerCustomBR(){
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.google.android.c2dm.intent.RECEIVE");
    filter.setPriority(1);
    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getExtras().getString("text");
            CharSequence text = "Message received in app: " + message;
            Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
            toast.show();
            Log.i(TAG, "Message received: "+message);
            ElementWave.messageReceived(context, intent);
            abortBroadcast();
         }
     };

     this.registerReceiver(receiver, filter);
     return receiver;
}

对android来说很新,所以我希望有一个简单的解决方案或任何我想念的明显的东西......

4

0 回答 0