Ad

Android - Adding An Intent To A Notification

I'm new on android development and I still don't understand Intent very well. Here i'm using Firebase to create notifications, I add an Intent to my notification, but when my app is in foreground and I click on the notification nothing happens (when the app is killed or in background it work well).

The strange thing is at a moment it was working, when I clicked on the notification the function "onNewIntent" of my "MainActivity" class was called, but now nothing happen anymore, and I think I changed nothing on the code.

Here is how I create my intent :

Notifications notifs = new Notifications(this);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
String url = remoteMessage.getData().get("url");

Intent intentNotif = new Intent(this, MainActivity.class);
intentNotif.setAction(Intent.ACTION_MAIN);
intentNotif.addCategory(Intent.CATEGORY_LAUNCHER);
intentNotif.putExtra("url", url);
intentNotif.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intentNotif.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

notifs.showNotif(title, body, true, intentNotif);

Here is how I add the intent to the notification :

this.builderGeneric.setContentIntent(PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));

I also tried to create an intent with a custom action but it's not working, I tryed different intent flags, but I feel like i'm trying random things without knowing what I do, so that's why I'm asking for your help.

EDIT : Here is how I create the notification if it's usefull :

Notifications(Context context) {
  this.context = context;

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    this.notifManager = context.getSystemService(NotificationManager.class);
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
    channel.setDescription(CHANNEL_DESCRIPTION);
    channel.enableLights(false);
    channel.enableVibration(false);
    channel.setSound(null, null);
    if (this.notifManager != null) {
      this.notifManager.createNotificationChannel(channel);
    }
  } else {
    this.notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  }

  this.builderGeneric = new Notification.Builder(context)
      .setVisibility(Notification.VISIBILITY_PUBLIC)
      .setWhen(System.currentTimeMillis())
      .setSmallIcon(R.mipmap.ic_launcher_round);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    this.builderGeneric.setChannelId(this.CHANNEL_ID);
  }
  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
    this.builderGeneric.setSmallIcon(R.mipmap.ic_launcher_foreground);
  }
}

public void showNotif(String title, String text, boolean cancelable, Intent intent) {
  this.builderGeneric.setContentTitle(title)
      .setContentText(text)
      .setOngoing(!cancelable)
      .setContentIntent(PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));

  Notification notifGeneric = this.builderGeneric.build();

  this.notifManager.notify(1, notifGeneric);
}

EDIT 2 : Here is my manifest :

<application
  android:allowBackup="true"
  android:fullBackupContent="@xml/backup_descriptor"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:launchMode="standard"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">

  <activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
  </activity>

  <receiver
    android:name=".NetworkChangeReceiver"
    android:label="NetworkChangeReceiver">
    <intent-filter
      android:name=".NetworkIntentFilter"
      android:label="NetworkIntentFilter">
      <action
        android:name="android.net.conn.CONNECTIVITY_CHANGE"
        tools:ignore="BatteryLife" />
      <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
  </receiver>

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

</application>
Ad

Answer

OK, First try to remove

intentNotif.setAction(Intent.ACTION_MAIN);
intentNotif.addCategory(Intent.CATEGORY_LAUNCHER);

Next, adding android:launchMode="singleTask" (also removing android:launchMode="standard") inside your activity tag in AndroidManifest.xml.

Then try again, please aware that with the launchMode is singleTask and if you click on your notification while your MainActivity is opened -> onNewIntent will be triggered (because Android will not create one more instance of this Activity) otherwise onCreate will be called.

And if it works, I would like to recommend you to read more about LaundMode here

Ad
source: stackoverflow.com
Ad