I have been engaged on a Flutter app the place it must help with the ability to faucet a notification and it takes you someplace within the app. Nonetheless, when the notification is tapped, it would not invoke any of the callbacks that I create. It simply opens the app usually to the house web page. I ship notifications from an Node.js backend and I take advantage of the firebase-admin
npm package deal to ship a notification.
I ship a notification like this:
/**
* Set off sending a notification
*/
const sendNotification = async ({
title,
physique,
information,
notificationType,
subject,
sendTo,
mediaUrl,
}) => {
let tokens = [];
if (sendTo) {
console.log(`sending focused notitification to: ${sendTo}`);
tokens = await getUserFCMTokens(sendTo);
}
const notification = {
information: {
...information,
notificationType: NOTIFICATION_TYPE[notificationType].toString(),
title: title,
physique: physique,
},
...(subject && { subject: subject }),
...(tokens.size > 0 && { tokens }),
apns: {
headers: {
'apns-priority': '5',
},
payload: {
aps: {
contentAvailable: true,
},
},
},
android: {
precedence: 'excessive',
notification: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
precedence: 'excessive',
sound: 'default',
},
},
};
console.log(notification);
strive {
if (tokens.size > 0) {
// Ship to a number of units
const response = await admin
.messaging()
.sendEachForMulticast(notification);
console.log(
`Efficiently despatched message to ${response.successCount} units`
);
if (response.failureCount > 0) {
console.log(
`Did not ship message to ${response.failureCount} units`
);
response.responses.forEach((resp, idx) => {
if (!resp.success) {
console.log(
`Did not ship to token at index ${idx}: ${resp.error}`
);
}
});
}
} else if (subject) {
// Ship to subject
const response = await admin.messaging().ship(notification);
console.log('Efficiently despatched message:', response);
} else {
console.log('No legitimate recipients (tokens or subject) specified');
}
} catch (error) {
console.error('Error sending notification:', error);
}
};
a notification would possibly appear to be this when it’s printed out:
{
information: {
group: '6716a89768497667e665546c',
media: '',
message: '6716a8d868497667e66554a4',
notificationType: '3',
title: 'title goes right here',
physique: '@zh22: Message goes right here '
},
subject: 'group_6716a89768497667e665546c',
apns: { headers: { 'apns-priority': '5' }, payload: { aps: [Object] } },
android: {
precedence: 'excessive',
notification: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
precedence: 'excessive',
sound: 'default'
}
}
}
Then, inside fundamental.dart
on the consumer facet, I initialize the Firebase background handler and in addition set the getInitialMessage
callback to open to a sure display screen within the app when the notification is clicked.
void fundamental() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(choices: DefaultFirebaseOptions.currentPlatform);
await AppData.provoke();
FirebaseMessaging.onMessage.pay attention((RemoteMessage message) {
print("onMessage: $message");
});
FirebaseMessaging.onMessageOpenedApp.pay attention((RemoteMessage message) {
print("onMessageOpenedApp: $message");
NotificationHandler.handleNotificationOnTap(message);
});
FirebaseMessaging.onBackgroundMessage(_onBackgroundMessage);
FirebaseMessaging.occasion
.getInitialMessage()
.then(NotificationHandler.handleNotificationOnTap);
runApp(
ShowCaseWidget(builder: (context) {
return const MyApp();
}),
);
}
@pragma("vm:entry-point")
Future _onBackgroundMessage(RemoteMessage message) async {
print("onBackgroundMessage: $message");
print("information: ${message.information}");
remaining NotificationRepository notificationRepository =
NotificationRepositoryImpl();
remaining FlutterLocalNotificationsPlugin localNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// present notification
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
"basecamp_notifications", "Basecamp Notifications",
significance: Significance.max,
precedence: Precedence.excessive,
ticker: 'ticker');
const DarwinNotificationDetails iosNotificationDetails =
DarwinNotificationDetails(
presentAlert: true,
presentSound: true,
interruptionLevel: InterruptionLevel.energetic);
int notificationId = 1;
const NotificationDetails platformSpecifics = NotificationDetails(
android: androidNotificationDetails, iOS: iosNotificationDetails);
await localNotificationsPlugin.initialize(
const InitializationSettings(
android: AndroidInitializationSettings("@mipmap/ic_launcher"),
iOS: DarwinInitializationSettings(),
),
onDidReceiveNotificationResponse: (NotificationResponse particulars) {
if (particulars.payload != null) {
remaining information = json.decode(particulars.payload!);
remaining message = RemoteMessage(information: Map.from(information));
NotificationHandler.handleNotificationOnTap(message);
}
},
);
remaining title = message.information["title"];
remaining physique = message.information["body"];
await localNotificationsPlugin.present(
notificationId, title, physique, platformSpecifics,
payload: message.information.toString());
}
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
print(notificationResponse);
// NotificationHandler.handleNotificationOnTap();
}
I attempted setting NotificationHandler.handleNotificationTap()
because the callback on each the native notification plugin’s onDidReceiveNotificationResponse
attribute and in addition with FirebaseMessaging.onBackgroundHandler
. Neither are being referred to as after I faucet the notification obtained on the bodily system.
After I get a notification from the backend, the output on the app facet seems like this:
flutter: onBackgroundMessage: Occasion of 'RemoteMessage'
flutter: information: {physique: @zh22: Message goes right here, message: 6716a8d868497667e66554a4, title: @title goes right here, group: 6716a89768497667e665546c, notificationType: 3, media: }
flutter: remoteMessage: null
discover that remoteMessage: null
is also printed when a notification is obtained, which I believe is unusual.
Lastly, after I faucet a notification, nothing is printed, main me to consider that getInitialMessage
is being not referred to as on the proper time, andonMessageOpenedApp
would not appear to be referred to as in any respect. I even have a print assertion inside NotificationHandler.handleNotificationTap()
that’s by no means printed on this entire course of, main me to consider that the operate is rarely entered.
Presently working this on iOS. Made certain to incorporate the Push Notification functionality and Xcode. FirebaseAppDelegateProxyEnabled
is about to NO.
Why is it that none of those callbacks are working after I faucet a notification?