how to handle push firebase notification click event when the application is in background?

二次信任 提交于 2019-12-11 04:47:53

问题


I'm New in Android.

When the application is open and notification came from firebase and click the notification then it open the Notification Activity but when the application is in foreground/background and notification came and click the notification then it automatically open the mainActivity.

I'm broadcasting the data from FirebaseMessagingService.class to Main Activity.

Why?

My code is below.

MainActivity

   public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            LocalBroadcastManager.getInstance(this)
                    .registerReceiver(mHandler,new IntentFilter("Pakage_name_FCM-MESSAGE"));
             }

        private BroadcastReceiver mHandler = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String title = intent.getStringExtra("title");
                String message = intent.getStringExtra("message");
                Log.d("MainActivity-title",title);
                Log.d("MainActivity-message",message); 
            }
        };

        @Override
        protected void onPause() {
            super.onPause();
            // Unregister the Broadcast receiver
 LocalBroadcastManager.getInstance(this).unregisterReceiver(mHandler);
        }
    }

FirebaseNotificationIdService.Class

public class FirebaseNotificationIdService extends FirebaseInstanceIdService {

    private static final String REG_TOKEN = "REG_TOKEN";

    @Override   // each time a new created and system call this method
    public void onTokenRefresh() {
        String recent_token= FirebaseInstanceId.getInstance().getToken();
        Log.d(REG_TOKEN,recent_token);
    }
}

FirebaseMessagingService.class

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d("Remote Form",remoteMessage.getFrom());

        // check if the message contains data...
        if (remoteMessage.getData().size()> 0){
               Log.d("Message Data", "Message Data---"+remoteMessage.getData() );

            String title=remoteMessage.getData().get("title");
            String message=remoteMessage.getData().get("message");

            Intent intent = new Intent("package_name_FCM-MESSAGE");
            intent.putExtra("title",title);
            intent.putExtra("message",message);
            Log.d("INTENT-TITLE",title);
            Log.d("INTENT-MESSAGE",message);
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.sendBroadcast(intent);
        }

        //check if the message contain notification

        if (remoteMessage.getNotification()!=null){
            Log.d("Message Body","---"+remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }

    //Display the notification
    private void sendNotification(String body) {
        Intent intent = new Intent(this,NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0/*Request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
        //Set sound
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this);
        notifiBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notifiBuilder.setContentTitle("Aadhaar Update");
        notifiBuilder.setContentText(body);
        notifiBuilder.setAutoCancel(true);
        notifiBuilder.setSound(notificationSound);
        notifiBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0/*Id of Notification*/,notifiBuilder.build());
    }
}

Minifest.xml

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".FirebaseNotificationIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".NotificationActivity"></activity>

回答1:


You should use implementation of WakefulBroadcastReceiver instead of FirebaseMessagingService

   public class NotificationsReceiver extends WakefulBroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //process data from intent
            abortBroadcast();
        }
   }

In your manifest

    <receiver
            android:name=".fcm.NotificationsReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter android:priority="999">
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
    </receiver>


来源:https://stackoverflow.com/questions/42694839/how-to-handle-push-firebase-notification-click-event-when-the-application-is-in

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