새 알림을 수신하면 조각을 열려고합니다. 이를 위해 나는 BroadcastReceiver를 사용한다. 문제는 sendNotification
메서드에서 내 액티비티에있는 메시지가 FirebaseMessagingService
클래스 안에없는 것입니다. 내 생각 엔이 문제는 IntentFilter를 onCreate
내부에 선언했으며 푸시 알림을 클릭 할 때 호출되지 않는다고 생각합니다. 이것을 할 수있는 다른 방법이 있습니까? Firebase onMessageReceived의 조각을 엽니 다.
답변
당신이 브로드 캐스트 리시버는 브로드 캐스트를 얻고 싶다면
// 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에 등록 된 모든 브로드 캐스트 리시버에 브로드 캐스트를 보낼 것입니다.
따라서 현재 구현에서는 활동이 다시 시작될 때 방송이 수신됩니다.
여전히 BroadcastReceiver @ wojciech의 onReceive 메서드에 도달하지 않습니다 –
FcmListenerService의 onMessageReceived 메서드에 도달 했습니까? 테스트 할 때 활동이 포 그라운드입니까? –
예, 알림을 클릭하면 HomeActivity가 열리고 있습니다. 그러나 BroadcastReceiver에 도달하지 못했습니다. @Wojciech –
- 1. 추가 정보가있는 다른 활동의 조각을 엽니 다.
- 2. PageAdapter에서 단편을 엽니 다.
- 3. 배경에서 iOS 앱을 엽니 다.
- 4. FCM - onMessageReceived의 배지 설정
- 5. SmsManager를 엽니 다
- 6. UIButton을 엽니 다. .mobileconfig
- 7. webview에서 팝업창을 엽니 다.
- 8. XmlWriter는 요소를 엽니 다.
- 9. cscript.exe가 창을 엽니 다.
- 10. 그래프 다이어그램을 엽니 다
- 11. 페이지로드시 토글을 엽니 다.
- 12. 항상 EntityManager를 엽니 다.
- 13. RadGrid의 PageIndex를 엽니 다.
- 14. 링크가있는 div를 엽니 다.
- 15. UIWebView가로드시 Safari를 엽니 다.
- 16. PHP 파일을 엽니 다.
- 17. 클릭하여 div를 엽니 다.
- 18. 문자열의 colorbox를 엽니 다.
- 19. tabbarcontroller보기를 엽니 다
- 20. 새 ViewController를 엽니 다.
- 21. iPhone 카메라를 엽니 다?
- 22. 데이터베이스에서 Xlsx를 엽니 다
- 23. ios 키보드를 엽니 다.
- 24. 브라우저에서 IManDocument를 엽니 다.
- 25. codebehind에서 jQuery Dialog를 엽니 다.
- 26. NetBeans로 파일을 드래그하여 엽니 다.
- 27. 브라우저에서 단어 문서를 엽니 다
- 28. C# - DotNetZip을 MemoryStream에서 엽니 다.
- 29. 런타임에 usercontrol을 엽니 다 (WPF?
- 30. Fancybox는 새 창을 엽니 다
왜 홈 액티비티 자체에서 브로드 캐스트 리시버를 사용 했습니까? 알림이 제대로 클릭되면 HomeActivity에 올 것입니까? – Jai
사촌 HomeActivity의 레이아웃에있는 FrameLayout을 사용하여 조각을 열어야합니다. –
하지만 이미 oncreate 메소드를 대체하고 있다면 방송 수신기의 필요성은 무엇입니까? – Jai