0

Hi i am working on calling app, but when someone call to me i got notification and onclick is working fine on notification, but i want to open activity without click on push notification, how it is possible because i try many codes but nothing works in Android 13.

My Broadcastreceiver is:-

public void onReceive(Context context, Intent intent) {
    if (intent != null && intent.getExtras() != null) {
        Intent openActivityIntent = new Intent(context, AudioViewReceiving.class);
        PendingIntent pi = PendingIntent.getActivity(context, 0, openActivityIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        try {
            pi.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
}

This is my FirebaseMessagingService class intent:-

Intent openActivityIntent = new Intent(this, AudioViewReceiving.class);
            openActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(openActivityIntent);

Manifest is:-

<service
        android:name=".Notification.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <receiver
        android:name=".AudioVideoCall.MessageReceiveBroadcast"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

This code is working is app is on Foreground but in background not, please suggest any idea for android 13, Thanks.

Amit Sharma
  • 261
  • 1
  • 4
  • 20

1 Answers1

0

Starting from Android 12 (API level 31), there have been some changes to background execution restrictions, especially related to launching activities from the background. To start an activity from the background in Android 12 and later, you should use the new TaskStackBuilder APIs.

Here's how you can modify your code to work on Android 13:

First, make sure you have updated your AndroidManifest.xml with the appropriate permissions for handling notifications.

In your BroadcastReceiver, instead of directly starting the activity, create a TaskStackBuilder and add the activity to the stack:

  public void onReceive(Context context, Intent intent) {
    if (intent != null && intent.getExtras() != null) {
        Intent openActivityIntent = new Intent(context, AudioViewReceiving.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntentWithParentStack(openActivityIntent);
        PendingIntent pi = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        try {
            pi.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
}

In your FirebaseMessagingService, instead of using startActivity directly, create a TaskStackBuilder there as well and add the activity to the stack:

  Intent openActivityIntent = new Intent(this, AudioViewReceiving.class);
openActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(openActivityIntent);

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
try {
    pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
    e.printStackTrace();
}

In your AndroidManifest.xml, make sure you have declared the appropriate permissions:

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Additionally, for improved background launching behavior, you may also need to add a element to your AndroidManifest.xml for the calling app:

 <queries>
    <package android:name="com.example.callingapp" />
</queries>