Skip to content

Replace flutter_local_notifications with pigeon bindings #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8

// The use of desugar_jdk_libs causes warning noise at build time:
// https://github.com/zulip/zulip-flutter/pull/887#issuecomment-2287653388
// https://issuetracker.google.com/issues/294273986
// TODO(#351): Try removing core-library desugaring once we've
// removed flutter_local_notifications.
coreLibraryDesugaringEnabled true
}

kotlinOptions {
Expand Down Expand Up @@ -103,5 +96,4 @@ flutter {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.4"
}
81 changes: 60 additions & 21 deletions android/app/src/main/kotlin/com/zulip/flutter/Notifications.g.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ data class NotificationChannel (
}
}

/**
* Corresponds to `android.content.Intent`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notif [nfc]: Replace PendingIntent.intentPayload with PendingIntent.intent

Hmm, tools/check pigeon fails for me at this commit; do you reproduce?

Running pigeon...
Error: there were changes to pigeons:
 M android/app/src/main/kotlin/com/zulip/flutter/Notifications.g.kt
 M lib/host/android_notifications.g.dart

FAILED: pigeon

But the commit after it passes. So maybe some generated code was committed in that later commit but should have been committed here.

*
* See:
* https://developer.android.com/reference/android/content/Intent
* https://developer.android.com/reference/android/content/Intent#Intent(java.lang.String,%20android.net.Uri,%20android.content.Context,%20java.lang.Class%3C?%3E)
*
* Generated class from Pigeon that represents data sent in messages.
*/
data class AndroidIntent (
val action: String,
val dataUrl: String,
/** A combination of flags from [IntentFlag]. */
val flags: Long

) {
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): AndroidIntent {
val action = __pigeon_list[0] as String
val dataUrl = __pigeon_list[1] as String
val flags = __pigeon_list[2].let { num -> if (num is Int) num.toLong() else num as Long }
return AndroidIntent(action, dataUrl, flags)
}
}
fun toList(): List<Any?> {
return listOf(
action,
dataUrl,
flags,
)
}
}

/**
* Corresponds to `android.app.PendingIntent`.
*
Expand All @@ -96,11 +130,7 @@ data class NotificationChannel (
*/
data class PendingIntent (
val requestCode: Long,
/**
* A value set on an extra on the Intent, and passed to
* the on-notification-opened callback.
*/
val intentPayload: String,
val intent: AndroidIntent,
/**
* A combination of flags from [PendingIntent.flags], and others associated
* with `Intent`; see Android docs for `PendingIntent.getActivity`.
Expand All @@ -112,15 +142,15 @@ data class PendingIntent (
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): PendingIntent {
val requestCode = __pigeon_list[0].let { num -> if (num is Int) num.toLong() else num as Long }
val intentPayload = __pigeon_list[1] as String
val intent = __pigeon_list[1] as AndroidIntent
val flags = __pigeon_list[2].let { num -> if (num is Int) num.toLong() else num as Long }
return PendingIntent(requestCode, intentPayload, flags)
return PendingIntent(requestCode, intent, flags)
}
}
fun toList(): List<Any?> {
return listOf(
requestCode,
intentPayload,
intent,
flags,
)
}
Expand Down Expand Up @@ -325,35 +355,40 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
}
130.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
PendingIntent.fromList(it)
AndroidIntent.fromList(it)
}
}
131.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
InboxStyle.fromList(it)
PendingIntent.fromList(it)
}
}
132.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
Person.fromList(it)
InboxStyle.fromList(it)
}
}
133.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
MessagingStyleMessage.fromList(it)
Person.fromList(it)
}
}
134.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
MessagingStyle.fromList(it)
MessagingStyleMessage.fromList(it)
}
}
135.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
Notification.fromList(it)
MessagingStyle.fromList(it)
}
}
136.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
Notification.fromList(it)
}
}
137.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
StatusBarNotification.fromList(it)
}
Expand All @@ -367,34 +402,38 @@ private object NotificationsPigeonCodec : StandardMessageCodec() {
stream.write(129)
writeValue(stream, value.toList())
}
is PendingIntent -> {
is AndroidIntent -> {
stream.write(130)
writeValue(stream, value.toList())
}
is InboxStyle -> {
is PendingIntent -> {
stream.write(131)
writeValue(stream, value.toList())
}
is Person -> {
is InboxStyle -> {
stream.write(132)
writeValue(stream, value.toList())
}
is MessagingStyleMessage -> {
is Person -> {
stream.write(133)
writeValue(stream, value.toList())
}
is MessagingStyle -> {
is MessagingStyleMessage -> {
stream.write(134)
writeValue(stream, value.toList())
}
is Notification -> {
is MessagingStyle -> {
stream.write(135)
writeValue(stream, value.toList())
}
is StatusBarNotification -> {
is Notification -> {
stream.write(136)
writeValue(stream, value.toList())
}
is StatusBarNotification -> {
stream.write(137)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
Expand Down
16 changes: 9 additions & 7 deletions android/app/src/main/kotlin/com/zulip/flutter/ZulipPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.zulip.flutter
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.annotation.Keep
Expand Down Expand Up @@ -97,13 +98,14 @@ private class AndroidNotificationHost(val context: Context)
contentIntent?.let { setContentIntent(
android.app.PendingIntent.getActivity(context,
it.requestCode.toInt(),
Intent(context, MainActivity::class.java).apply {
// This action name and extra name are special to
// FlutterLocalNotificationsPlugin, which handles receiving the Intent.
// TODO take care of receiving the notification-opened Intent ourselves
action = "SELECT_NOTIFICATION"
putExtra("payload", it.intentPayload)
},
it.intent.let { intent -> Intent(
intent.action,
Uri.parse(intent.dataUrl),
context,
MainActivity::class.java
).apply {
flags = intent.flags.toInt()
} },
it.flags.toInt())
) }
contentText?.let { setContentText(it) }
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/raw/keep.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is necessary for notifications to work in release builds.

The issue is that the package:flutter_local_notifications API has us
The issue is that the Pigeon bindings for notifications API has us
identify which icon we want to use via a string name, in Dart code,
and that's invisible to the resource shrinker:
https://developer.android.com/build/shrink-code#keep-resources
Expand Down
8 changes: 8 additions & 0 deletions assets/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -558,5 +558,13 @@
"pollWidgetOptionsMissing": "This poll has no options yet.",
"@pollWidgetOptionsMissing": {
"description": "Text to display for a poll when it has no options"
},
"errorNotificationOpenTitle": "Failed to open notification",
"@errorNotificationOpenTitle": {
"description": "Error title when notification opening fails"
},
"errorNotificationOpenAccountMissing": "The account associated with this notification no longer exists.",
"@errorNotificationOpenAccountMissing": {
"description": "Error message when the account associated with the notification is not found"
}
}
6 changes: 0 additions & 6 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ PODS:
- GoogleUtilities/UserDefaults (~> 7.8)
- nanopb (< 2.30911.0, >= 2.30908.0)
- Flutter (1.0.0)
- flutter_local_notifications (0.0.1):
- Flutter
- GoogleDataTransport (9.4.1):
- GoogleUtilities/Environment (~> 7.7)
- nanopb (< 2.30911.0, >= 2.30908.0)
Expand Down Expand Up @@ -155,7 +153,6 @@ DEPENDENCIES:
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`)
- Flutter (from `Flutter`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- integration_test (from `.symlinks/plugins/integration_test/ios`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
Expand Down Expand Up @@ -196,8 +193,6 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/firebase_messaging/ios"
Flutter:
:path: Flutter
flutter_local_notifications:
:path: ".symlinks/plugins/flutter_local_notifications/ios"
image_picker_ios:
:path: ".symlinks/plugins/image_picker_ios/ios"
integration_test:
Expand Down Expand Up @@ -231,7 +226,6 @@ SPEC CHECKSUMS:
FirebaseInstallations: 913cf60d0400ebd5d6b63a28b290372ab44590dd
FirebaseMessaging: 7b5d8033e183ab59eb5b852a53201559e976d366
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_local_notifications: 4cde75091f6327eb8517fa068a0a5950212d2086
GoogleDataTransport: 6c09b596d841063d76d4288cc2d2f42cc36e1e2a
GoogleUtilities: ea963c370a38a8069cc5f7ba4ca849a60b6d7d15
image_picker_ios: c560581cceedb403a6ff17f2f816d7fea1421fc1
Expand Down
21 changes: 16 additions & 5 deletions lib/api/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ sealed class FcmMessageWithIdentity extends FcmMessage {
///
/// This is a real, absolute URL which is the base for all URLs a client uses
/// with this realm. It corresponds to [GetServerSettingsResult.realmUri].
final Uri realmUri;
@JsonKey(readValue: _readRealmUrl) // TODO(server-9)
final Uri realmUrl;

/// This user's ID within the server.
///
Expand All @@ -75,9 +76,14 @@ sealed class FcmMessageWithIdentity extends FcmMessage {
FcmMessageWithIdentity({
required this.server,
required this.realmId,
required this.realmUri,
required this.realmUrl,
required this.userId,
});

// TODO(server-9): FL 257 deprecated 'realm_uri' in favor of 'realm_url'.
static String _readRealmUrl(Map<dynamic, dynamic> json, String key) {
return (json['realm_url'] ?? json['realm_uri']) as String;
}
}

/// Parsed version of an FCM message of type `message`.
Expand Down Expand Up @@ -117,7 +123,7 @@ class MessageFcmMessage extends FcmMessageWithIdentity {
MessageFcmMessage({
required super.server,
required super.realmId,
required super.realmUri,
required super.realmUrl,
required super.userId,
required this.senderId,
required this.senderAvatarUrl,
Expand Down Expand Up @@ -147,6 +153,7 @@ class MessageFcmMessage extends FcmMessageWithIdentity {
if (recipient.streamName != null) result['stream'] = recipient.streamName;
result['topic'] = recipient.topic;
}
result['realm_uri'] = realmUrl.toString(); // TODO(server-9): deprecated in FL 257
return result;
}
}
Expand Down Expand Up @@ -236,7 +243,7 @@ class RemoveFcmMessage extends FcmMessageWithIdentity {
RemoveFcmMessage({
required super.server,
required super.realmId,
required super.realmUri,
required super.realmUrl,
required super.userId,
required this.zulipMessageIds,
});
Expand All @@ -247,7 +254,11 @@ class RemoveFcmMessage extends FcmMessageWithIdentity {
}

@override
Map<String, dynamic> toJson() => _$RemoveFcmMessageToJson(this);
Map<String, dynamic> toJson() {
final result = _$RemoveFcmMessageToJson(this);
result['realm_uri'] = realmUrl.toString(); // TODO(server-9): deprecated in FL 257
return result;
}
}

class _IntListConverter extends JsonConverter<List<int>, String> {
Expand Down
10 changes: 6 additions & 4 deletions lib/api/notifications.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/host/android_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ abstract class PendingIntentFlag {
/// Corresponds to `FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT`.
static const allowUnsafeImplicitIntent = 1 << 24;
}

/// For use in [AndroidIntent.action].
///
/// See: https://developer.android.com/reference/android/content/Intent#constants_1
abstract class IntentAction {
/// Corresponds to `ACTION_VIEW`.
static const view = 'android.intent.action.VIEW';
}

/// For use in [AndroidIntent.flags].
///
/// See: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_BROUGHT_TO_FRONT
abstract class IntentFlag {
/// Corresponds to `FLAG_ACTIVITY_CLEAR_TOP`.
static const activityClearTop = 1 << 26;

/// Corresponds to `FLAG_ACTIVITY_NEW_TASK`.
static const activityNewTask = 1 << 28;
}
Loading