0

새 알림을 수신하면 조각을 열려고합니다. 이를 위해 나는 BroadcastReceiver를 사용한다. 문제는 sendNotification 메서드에서 내 액티비티에있는 메시지가 FirebaseMessagingService 클래스 안에없는 것입니다. 내 생각 엔이 문제는 IntentFilter를 onCreate 내부에 선언했으며 푸시 알림을 클릭 할 때 호출되지 않는다고 생각합니다. 이것을 할 수있는 다른 방법이 있습니까? Firebase onMessageReceived의 조각을 엽니 다.

+0

왜 홈 액티비티 자체에서 브로드 캐스트 리시버를 사용 했습니까? 알림이 제대로 클릭되면 HomeActivity에 올 것입니까? – Jai

+0

사촌 HomeActivity의 레이아웃에있는 FrameLayout을 사용하여 조각을 열어야합니다. –

+0

하지만 이미 oncreate 메소드를 대체하고 있다면 방송 수신기의 필요성은 무엇입니까? – Jai

답변

0

당신이 브로드 캐스트 리시버는 브로드 캐스트를 얻고 싶다면

// Globally declared 
 
\t private IntentFilter mIntentFilter; 
 
\t 
 
\t @Override 
 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_home); 
 
\t \t 
 
\t \t // Open a fragment automatically 
 
\t \t if (findViewById(R.id.frame) != null) { 
 
      if (savedInstanceState != null) { 
 
       return; 
 
      } 
 

 
      myFragmentManager = getSupportFragmentManager(); 
 
      myFragmentTransaction = myFragmentManager.beginTransaction(); 
 
      myFragmentTransaction.replace(R.id.frame, new ProdList()).commit(); 
 
     } 
 
     
 

 
     mIntentFilter = new IntentFilter(); 
 
     mIntentFilter.addAction("com.sam.CUSTOM_INTENT"); 
 

 

 
    } 
 
\t 
 
\t @Override 
 
    protected void onResume() { 
 
     super.onResume(); 
 
     registerReceiver(mReceiver, mIntentFilter); 
 
    } 
 

 
    @Override 
 
    protected void onPause() { 
 
     super.onPause(); 
 
     unregisterReceiver(mReceiver); 
 
    } 
 
\t 
 
\t private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
 
     @Override 
 
     public void onReceive(Context context, Intent intent) { 
 

 
      if (intent.getAction().equals("com.sam.CUSTOM_INTENT")) { 
 

 
       Toast.makeText(getApplicationContext(), "GOT MESSAGE", Toast.LENGTH_SHORT).show(); 
 
       InventoryList il = new InventoryList(); 
 
       replaceFragment(il,"IL"); 
 
      } 
 
     } 
 
    };
@Override 
 
    public void onMessageReceived(RemoteMessage remoteMessage) {   
 

 
     if (remoteMessage.getData().size() > 0) { 
 
      for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) { 
 
       
 
       String value = entry.getValue(); 
 
       
 
       try { 
 
        JSONObject obj = new JSONObject(value); 
 
        
 
        sendNotification(obj.getString("message"),"SUCCESS"); 
 
        
 

 
       } catch (JSONException e) { 
 
        e.printStackTrace(); 
 
       } 
 
      } 
 

 
     } 
 

 

 
    } 
 
\t 
 
\t 
 
\t private void sendNotification(String messageBody, String param) { 
 
     Intent intent = new Intent(this, HomeActivity.class); 
 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
 
\t \t intent.setAction("MESSAGE"); 
 

 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
 
       PendingIntent.FLAG_ONE_SHOT); 
 

 
     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
 
       .setSmallIcon(R.mipmap.ic_com.sam.CUSTOM_INTENTr) 
 
       .setContentTitle("Nika") 
 
       .setContentText(messageBody) 
 
       .setAutoCancel(true) 
 
       .setSound(defaultSoundUri) 
 
       .setContentIntent(pendingIntent); 
 

 
     NotificationManager notificationManager = 
 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
 

 
     notificationManager.notify(0, notificationBuilder.build()); 
 
    }

, 당신은 실제로 방송을 보낼 수 있습니다. 당신의 onMessageReceived()에이 코드를 넣어 :

Intent intent = new Intent("com.sam.CUSTOM_INTENT"); 
context.sendBroadcast(intent); 

이 조각을 행동 "com.sam.CUSTOM_INTENT" 일치로 IntentFilter에 등록 된 모든 브로드 캐스트 리시버에 브로드 캐스트를 보낼 것입니다.

따라서 현재 구현에서는 활동이 다시 시작될 때 방송이 수신됩니다.

+0

여전히 BroadcastReceiver @ wojciech의 onReceive 메서드에 도달하지 않습니다 –

+0

FcmListenerService의 onMessageReceived 메서드에 도달 했습니까? 테스트 할 때 활동이 포 그라운드입니까? –

+0

예, 알림을 클릭하면 HomeActivity가 열리고 있습니다. 그러나 BroadcastReceiver에 도달하지 못했습니다. @Wojciech –