2

クリックされた通知の要件: 通知のクリック時に IntentReceiver.java クラスを処理 (実行) してから、インテントを転送したい。

IntentReceiver.java(BroadcastReceiver のサブクラス) ---> Notification.java(アクティビティ) ---> メイン ダッシュボード(アクティビティ)

1.) 私のアプリケーションでは、"BroadcastReceiver" のサブクラスである別のクラス "IntentReceiver.java" があります。

2.) その後、「IntentReceiver.java」クラス内で、レイアウト画面のない「Notification.java」クラスに切り替え、データを操作してメイン ダッシュボードに切り替えます。

3.) このメイン ダッシュボードでは、操作後に "Notification .java" クラスからメイン ダッシュボードで (putExtra() を介して) 受信したさまざまなキーに代わって、さまざまなダイアログを処理します。

IntentReceiver.java クラスのコード :これは、各通知を処理する個別のクラスです。

public class IntentReceiver extends BroadcastReceiver {
        Context ctx;

        private static String PUSH_KEY_ALERT = "alert";

   @Override

        public void onReceive(Context context, Intent intent) {

            this.ctx = context;

        String alert = intent.getStringExtra(PUSH_KEY_ALERT);

        Bundle extras = getResultExtras(true);

        extras.putInt(PushIOManager.PUSH_STATUS, PushIOManager.PUSH_HANDLED_NOTIFICATION);

        setResultExtras(extras);

    }

}

マニフェスト構成:

<receiver android:name="com.DxS.android.push.IntentReceiver" > </receiver>



<activity android:name=".Notification">

        <action android:name="com.DxS.android.NOTIFICATIONPRESSED" />

        <category android:name="android.intent.category.DEFAULT" />

 </activity>



 <activity android:name=".dashboard"> </activity>

これが私の要求の流れです。それを行うための最良の方法を教えてください。前もって感謝します...

4

1 に答える 1

2

まず、onReceiveメソッドでエラーをチェックする必要があります。次のコードは通知を表示しNotification、通知がタップされるとアクティビティを開始します。レイアウトがない場合、通知アクティビティの目的がわかりません。dashboardおそらくアクティビティである必要はなく、通知をタップするとアクティビティが直接開始されるはずです。

public class IntentReceiver extends BroadcastReceiver {
   static final String TAG = "IntentReceiver";
   Context ctx;

   @Override
   public void onReceive(Context context, Intent intent) {
       GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
       ctx = context;
       String messageType = gcm.getMessageType(intent);
       if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) 
           Log.i(TAG, "PUSH RECEIVED WITH ERROR: " + intent.getExtras().toString());
       else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) 
           Log.i(TAG, "DELETED PUSH MESSAGE: " + intent.getExtras().toString());
       else
       {
           Log.i(TAG, "Received PUSH: " + intent.getExtras().toString());
           postNotification(intent.getExtras());
       }
       setResultCode(Activity.RESULT_OK);
   }

  // post GCM message to notification center.
  private void postNotification(Bundle data) {
     String msg = data.getString("alert");
     Log.i(TAG, "message: " + msg);

     Intent intent = new Intent(ctx, Notification.class);
     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, 0);

     NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
     .setContentTitle("Your Title")
     .setContentText(msg)
     .setTicker(msg)
     .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
     .setAutoCancel(true)
     .setOnlyAlertOnce(true)
     .setDefaults(Notification.DEFAULT_VIBRATE); 

     builder.setContentIntent(contentIntent);
     NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(0, builder.build());
  }
}
于 2013-10-24T15:25:43.213 に答える