-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement handling of Android actions in background
There are some cases when local notification action should be handled in background eg. snoozing the reminder. In case of it launching app UI is not necessary and would be confusing for the end user. Therefore there should be a way to handle local notification action in background. For this reason new property 'runInBackground' was added to the AndroidAction class and TypeScript type. Also new broadcast receiver and service were implemented to handle properly background actions. In order to run particular action in background API consumer need to set its 'runInBackground' property to 'true', eg: ... .android.addAction(new firebase.notifications.Android.Action("snooze", "ic_snooze", "Snooze").setRunInBackground(true)) ... Then, there are two cases that API consumer needs to handle. First when app is in the foreground, standard notification and notification action code path will be executed. This mean, that: * onNotification() listener will be called (which should call displayNotification(), in order to show it to the user), * onNotificationOpen() listener will be called after the action is tapped by the user Secondly, when application is in background or it is not running new 'RNFirebaseBackgroundNotificationAction' handler will be called. To properly handle this case API consumer should create a background asynchronous handler: const handleAsyncTask = async (notificationOpen: NotifficationOpen) => { if (notificationOpen && notificationOpen.notification) { const action = notificationOpen.action; const notificationId = notificationOpen.notification.notificationId; if (action === "snooze") { console.log("Reschedule notification for later time", notificationId); } else { console.log("unsupported action", action); } // hide the notification firebase.notifications().removeDeliveredNotification(notificationId); } } Next hander should be registered to headless handler: AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', () => handleAsyncTask); Finally AndroidManifest.xml file must be modified, to include receiver and service definition: <receiver android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionReceiver" android:exported="true"> <intent-filter> <action android:name="io.invertase.firebase.notifications.BackgroundAction"/> </intent-filter> </receiver> <service android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionsService"/> Now when ever 'Snooze' action is pressed it will launch 'handleAsyncTask' function in the background or onNotificationOpen() when app is in foreground. And reschedule the notification for the later time.
- Loading branch information
Showing
6 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...a/io/invertase/firebase/notifications/RNFirebaseBackgroundNotificationActionReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.invertase.firebase.notifications; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
|
||
import com.facebook.react.HeadlessJsTaskService; | ||
import com.facebook.react.ReactApplication; | ||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.react.bridge.WritableMap; | ||
|
||
import io.invertase.firebase.Utils; | ||
|
||
public class RNFirebaseBackgroundNotificationActionReceiver extends BroadcastReceiver { | ||
static boolean isBackgroundNotficationIntent(Intent intent) { | ||
return intent.getExtras() != null && intent.hasExtra("action") && intent.hasExtra("notification"); | ||
} | ||
|
||
static WritableMap toNotificationOpenMap(Intent intent) { | ||
Bundle extras = intent.getExtras(); | ||
WritableMap notificationMap = Arguments.makeNativeMap(extras.getBundle("notification")); | ||
WritableMap notificationOpenMap = Arguments.createMap(); | ||
notificationOpenMap.putString("action", extras.getString("action")); | ||
notificationOpenMap.putMap("notification", notificationMap); | ||
return notificationOpenMap; | ||
} | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
if (!isBackgroundNotficationIntent(intent)) { | ||
return; | ||
} | ||
|
||
if (Utils.isAppInForeground(context)) { | ||
WritableMap notificationOpenMap = toNotificationOpenMap(intent); | ||
|
||
ReactApplication reactApplication = (ReactApplication)context.getApplicationContext(); | ||
ReactContext reactContext = reactApplication.getReactNativeHost().getReactInstanceManager().getCurrentReactContext(); | ||
|
||
Utils.sendEvent(reactContext, "notifications_notification_opened", notificationOpenMap); | ||
} else { | ||
Intent serviceIntent = new Intent(context, RNFirebaseBackgroundNotificationActionsService.class); | ||
serviceIntent.putExtras(intent.getExtras()); | ||
context.startService(serviceIntent); | ||
HeadlessJsTaskService.acquireWakeLockNow(context); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...a/io/invertase/firebase/notifications/RNFirebaseBackgroundNotificationActionsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.invertase.firebase.notifications; | ||
|
||
import android.content.Intent; | ||
|
||
import com.facebook.react.HeadlessJsTaskService; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.jstasks.HeadlessJsTaskConfig; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import static io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionReceiver.isBackgroundNotficationIntent; | ||
import static io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionReceiver.toNotificationOpenMap; | ||
|
||
public class RNFirebaseBackgroundNotificationActionsService extends HeadlessJsTaskService { | ||
@Override | ||
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) { | ||
if (isBackgroundNotficationIntent(intent)) { | ||
WritableMap notificationOpenMap = toNotificationOpenMap(intent); | ||
|
||
return new HeadlessJsTaskConfig( | ||
"RNFirebaseBackgroundNotificationAction", | ||
notificationOpenMap, | ||
60000, | ||
true | ||
); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters