open fragment from Firebase onMessageReceived

Deadly 提交于 2019-12-11 05:57:13

问题


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

回答1:


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

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.



来源:https://stackoverflow.com/questions/40211624/open-fragment-from-firebase-onmessagereceived

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!