1

The code to open application upon receiving notification is working till Android 9 but not in 10. Android has some major security changes in Android 10 as per their document. I don't know how this can be done in the Android 10.

This is my code.

FirebaseNotificationService.class

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        dataMap = remoteMessage.getData();
        if (dataMap == null) {
            return;
        }
       if (dataMap.get("title").equalsIgnoreCase("video_call")) {
            Intent intent = new Intent(AppConstants.INCOMING_CALL_BROADCAST_ACTION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("title", remoteMessage.getData().get("title"));
            intent.putExtra("sender", remoteMessage.getData().get("sender"));
            sendImplicitBroadcast(FirebaseNotificationService.this, intent);
        }
    }

private static void sendImplicitBroadcast(Context ctxt, Intent i) {
        PackageManager pm = ctxt.getPackageManager();
        List<ResolveInfo> matches = pm.queryBroadcastReceivers(i, 0);

        for (ResolveInfo resolveInfo : matches) {
            Intent explicit = new Intent(i);
            ComponentName cn =
                    new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
                            resolveInfo.activityInfo.name);

            explicit.setComponent(cn);
            ctxt.sendBroadcast(explicit);
        }
    }

This i'm receiving in my Service receiver class and open an activity.

VideoCallReceiver.class

 @Override
    public void onReceive(Context context, Intent intent) {
        if (Objects.requireNonNull(intent.getAction()).equalsIgnoreCase(AppConstants.INCOMING_CALL_BROADCAST_ACTION)) {
            context.startActivity(new Intent(context, ReceiveCallActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .setAction(Intent.ACTION_ANSWER)
                    .putExtra("title", intent.getStringExtra("title"))
                    .putExtra("sender", intent.getStringExtra("sender"))
        } 
    }

The declaration of receiver in manifest is as such

AndroidManifest.xml

<receiver
            android:name=".services.VideoCallReceiver"
            android:excludeFromRecents="true"
            android:showWhenLocked="true"
            android:turnScreenOn="true">
            <intent-filter>
                <action android:name="call_from_user" />
            </intent-filter>
        </receiver>

This code has been tested till Android 9. All working fine.

Any help would be appreciated.

Shahal
  • 1,008
  • 1
  • 11
  • 29
  • 1
    You have random attributes in your `` [that do not belong there](https://developer.android.com/guide/topics/manifest/receiver-element). You have security problems, by broadcasting to anyone who has a receiver for your action. And, even when all of this "works", you take over the foreground with your activity, which may cause serious problems for the user. You should be using a high-priority notification with a full-screen intent on all modern versions of Android. Android 10 just forces you to do so. – CommonsWare Sep 29 '19 at 14:30
  • @CommonsWare I've corrected the attributes. Everything is working fine now. I used [https://stackoverflow.com/questions/57833208/cant-start-activity-from-broadcastreceiver-on-android-10]. But i'm stuck at ringtone now. Not able to use custom ringtone. Every time it's default notification tone. – Shahal Sep 30 '19 at 13:37

0 Answers0