android - 从 Firebase onMessageReceived 打开 fragment

标签 android firebase-cloud-messaging android-broadcastreceiver

我正在尝试在收到新通知时打开 fragment 。为此,我使用 BroadcastReceiver。问题是我没有从 FirebaseMessagingService 类中的 sendNotification 方法在我的 Activity 中获取消息。我认为问题是我在 onCreate 中声明了 IntentFilter,当我点击推送通知时它没有被调用。还有其他方法吗?

// Globally declared
	private IntentFilter mIntentFilter;
	
	@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
		
		// Open a fragment automatically
		 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");


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

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }
	
	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();
                }
            }

        }


    }
	
	
	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);
		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());
    }

最佳答案

如果想让BroadcastReceiver得到广播,就得实际发送Broadcast。将此代码放入您的 onMessageReceived():

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

此代码段会将广播发送到所有已注册的具有 IntentFilter 匹配操作 "com.sam.CUSTOM_INTENT" 的 BroadcastReceivers。

因此,在当前的实现中,您的 Activity 将在恢复时进行广播。

关于android - 从 Firebase onMessageReceived 打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40211624/

相关文章:

objective-c - Firebase FCM 实例 ID 为零(在真实设备上运行快速启动应用程序)

firebase - FCM 到多个设备( token )列表

android - 使用自定义 broadcastreceiver 接收 Parse 通知

java - 无法一次从节点 "History"获取一个 key

Android BluetoothLeScanner startScan PendingIntent 28 次后在系统范围内失败

java - 适用于 Java 的 Google-API 日历提要

android - 无法从小米设备的后台唤醒应用程序,直到手动启用 "Autostart"

android - 无法调试白名单设备/ApplicationSession 无法启动

java - Android Notification Not getting Sound- FCM 使用 PHP

android - 如何仅在连接互联网时运行服务?