2017-02-01 10 views
0

firebaseからAndroidアプリケーションへの通知を送信するためのチュートリアルを2回続けてきましたが、問題は発生しません。どのアクティビティでもfirebase通知を聞く方法

私は2つのことを理解しようとしています:ユーザーが通知を処理しない活動を見ている間、私は、通知の受信を処理するにはどうすればよい

  1. を?

  2. アプリケーションが実行中だがフォアグラウンドではなく、通知を受け取ったとき、システムトレイからクリックすると、アプリケーションがブロードキャスターが定義されているアクティビティを開く - どのアクティビティを開くかを制御する?ここで

は、コードの他の部分は、私に知らせて確認する必要がある場合は、通知

public class DisplayNotificationActivity extends AppCompatActivity { 
    private BroadcastReceiver mRegistrationBroadcastReceiver; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_notification); 

     mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      // checking for type intent filter 
      if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) { 
       // gcm successfully registered 
       // now subscribe to `global` topic to receive app wide notifications 
       FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL); 
      } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) { 
       // new push notification is received 
       String message = intent.getStringExtra("message"); 
       Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show(); 
       txtMessage.setText(message); 
      } 
     } 
    }; 

を扱う活動からのコードです。

答えて

0

ユーザーが通知を処理していないアクティビティを表示しているときに、通知の受信を処理するにはどうすればよいですか。

私は可能な限り短くしようとします。あなたのアプリがフォアグラウンドにあるとき、FirebaseMessagingServiceの実装のonMessageReceived()メソッドで通知が届きます。今あなたはどのアクティビティをイベントにブロードキャストする必要がありますか?基本アクティビティクラスを作成し、すべてのアクティビティでそれを継承させる。

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) 
public void onNotificationReceived(NotificationReceieveBean bean){ 

if(bean != null){ 

     ViewUtils.showJobOfferSnackBar(this, bean.map); 

     EventBus.getDefault().removeStickyEvent(bean); 
    } 



} 

ベースの活動を拡張すべての活動は、通知イベントを受け取ることができるようになります:私はこのようなないようBaseActivityでは、ブロードキャスト・アプローチを使用するか、いくつかのEventbusライブラリを使用して試すことができます。それに応じてそれを処理することができます。

アプリが実行されているが、フォアグラウンドではないと私はシステムからそれをクリックしたとき、私は、通知を受け取るアプリケーションが定義された放送局を持っているアクティビティを開きトレイ - どのように私は、ある活動を制御します開いた?

通常、システム生成通知をクリックすると、最初のアクティビティが開きます。あなたはのonCreate()の例では、あなたがサーバーから送信しているものと同じキーを持つBundleインスタンス内のデータをチェックする必要があります。

Bundle bundle = getIntent().getExtras(); 

    if(bundle != null){ 
     // if the below condition is true then it means you have received notification 
     if(bundle.getString("notification_key_sent_from_server") != null) { 
      ViewUtils.handleIncomingFCMNotification(bundle); 

      finish(); // finishing the first activity since I want to go to some other activity 
      return; 

     } 

    } 

** EDIT **

public abstract class BaseNotificationHandlerActivity extends AppCompatActivity { 

protected int activityFlag = 0; // set a unique value from each extending Activity 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
} 

@Override 
public void onStart(){ 
    super.onStart(); 
    EventBus.getDefault().register(this); 
} 

    @Override 
    public void onStop() { 
    super.onStop(); 
    EventBus.getDefault().unregister(this); 
} 

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onMessageEvent(NotificationItem notif) { 
    if(activityFlag == 1){ 
    // this means Starting Activity, so handle for it particularly 
     NotificationHelper notifHelper = new NotificationHelper(this); 
     notifHelper.AddNotificationItem(notif); 
    } 
    else if(activityFlag ==2){ 
    // for some other Activity 
    } 

    } 
} 


public class StartingActivity extends BaseNotificationHandlerActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_starting); 
    activityFlag = 1; // add unique flag for Activity here 
} 

@Override 
public void onResume() { 
super.onResume(); 
} 
+0

OK - 質問1を参照して、特定のアクティビティで通知の受信を別々に処理するにはどうすればよいですか? – andrewb

+0

@andrewb、ちょうど通知を処理するBaseActivityのメソッドを作成します。実装は、別の方法で通知を処理したい場合、そのメソッドをオーバーライドする必要があります。 –

+0

私の場合、BaseActivit、yはこのDisplayNotificationActivityですか? – andrewb

関連する問題