0

in android, it can setup the deeplink to launch an Activity when receives the push notification.

        <activity
            android:name=".ActivityForTestAction"
            android:launchMode="singleTop"
            >
           <intent-filter >
                <action android:name="demoapp.action.WRITE_TO_DATABASE" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:scheme="demoapp"
                    android:host="www.demoapp.com"
                    android:pathPrefix="/test" />
            </intent-filter>

and it will just build the notification with "demoapp.action.WRITE_TO_DATABASE" and proper Uri:

        Intent intent = new Intent("demoapp.action.WRITE_TO_DATABASE");
        intent.setData(Uri.parse("demoapp://www.demoapp.com/test"));

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        
        builder = new Notification.Builder(context)
            .setContentIntent(pendingIntent)
            ... ...;

        Notification notification = builder.build();
        notificationManager.notify(100, notification);

it will open the ActivityForTestAction if tap on the notification.

however, there is case that some of the push notification does not need any ui (Activity), for example, just need to write to database.

So is there a way to still do the "deeplink" but instantiate the class and run the action there?

lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

1

If you don't want any activity to get opened, you can simply use a broadcast receiver. change this part like this:

    val intent = Intent(context,NotificationReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
        cotext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT
    )

then on your BroadcastReceiver onReceive method you can do whatever you want without opening any activity.

Maede Jalali
  • 306
  • 2
  • 6