从Firebase onMessageReceived打开片段 [英] open fragment from Firebase onMessageReceived

查看:73
本文介绍了从Firebase onMessageReceived打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在收到新通知时打开一个片段。为此,我使用了BroadcastReceiver。问题是我没有从 FirebaseMessagingService 类中的 sendNotification 方法获得活动消息。我认为问题是我在 onCreate 中声明了IntentFilter,并且当我单击推送通知时未调用它。还有其他方法吗?

I'm trying to open a fragment when a new Notification is received. For this I use a BroadcastReceiver. Problem is I'm not getting the message in my Activity from sendNotification method inside FirebaseMessagingService class. I think the issue is I declared the IntentFilter inside onCreate and its not being called when I click on the push notification. Is there any other way to do this?

// 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获得广播,您必须实际发送广播。将此代码放入您的 onMessageReceived()

If you want BroadcastReceiver to get the broadcast, you have to actually send the Broadcast. Put this code in your onMessageReceived():

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

此代码段会将广播发送给所有已注册IntentFilter匹配操作的BroadcastReceiver com.sam.CUSTOM_INTENT

This snippet will send the broadcast to all registered BroadcastReceivers with IntentFilter matching action "com.sam.CUSTOM_INTENT".

因此,在当前的实现中,您的活动将在恢复后广播。

So, with current implementation, your activity will get broadcast when it is resumed.

这篇关于从Firebase onMessageReceived打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆