A new Flutter project for IOS Notification using apns token
The flutter_apns plugin has been migrated and deprecated issue Resolved 👍
✅ DEPRECATED ISSUE RESOLVED ✅
This package is not longer maintained. Please write message or GitHub issue if you want to take it over and end deprecated state.
Plugin to implement APNS push notifications on iOS and Firebase on Android.
Currently, the only available push notification plugin is firebase_messaging
. This means that, even on iOS, you will need to setup firebase and communicate with Google to send push notification. This plugin solves the problem by providing native APNS implementation while leaving configured Firebase for Android.
Configure firebase on Android according to instructions: https://pub.dartlang.org/packages/firebase_messaging.
On iOS, make sure you have correctly configured your app to support push notifications, and that you have generated certificate/token for sending pushes. For more infos see section How to run example app on iOS
Add the following lines to the didFinishLaunchingWithOptions
method in the AppDelegate.m/AppDelegate.swift file of your iOS project
Objective-C:
if (@available(iOS 11.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
Swift:
if #available(iOS 11.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
flutter_apns
as a dependency in your pubspec.yaml file.createPushConnector()
method, configure push service according to your needs. PushConnector
closely resembles FirebaseMessaging
, so Firebase samples may be useful during implementation. You should create the connector as soon as possible to get the onLaunch callback working on closed app launch.
import 'package:flutter_apnsX/flutter_apns/flutter_apns_only_x.dart';
final connector = createPushConnector();
connector.configureApns( onLaunch: (data) => onPush('onResume', data.payload.values.single), onResume: (data) => onPush('onResume', data.payload.values.single), onMessage: (data) => onPush('onResume', data.payload.values.single), onBackgroundMessage: (data) => onPush('onMessage', data.payload.values.single,) ); //requestNotificationPermissions// connector.requestNotificationPermissions() //token value get// connector.token.addListener(() { print('Token ${connector.token.value}'); });
6. Build on device and test your solution using Firebase Console (Android) and CURL (iOS, see [How to run example app on iOS](#how-to-run-example-app-on-ios)).
## Additional APNS features:
### Displaying notification while in foreground
```dart
final connector = createPushConnector();
if (connector is ApnsPushConnector) {
connector.shouldPresent = (x) => Future.value(true);
}
Firstly, configure supported actions:
final connector = createPushConnector();
if (connector is ApnsPushConnector) {
connector.setNotificationCategories([
UNNotificationCategory(
identifier: 'MEETING_INVITATION',
actions: [
UNNotificationAction(
identifier: 'ACCEPT_ACTION',
title: 'Accept',
options: [],
),
UNNotificationAction(
identifier: 'DECLINE_ACTION',
title: 'Decline',
options: [],
),
],
intentIdentifiers: [],
options: [],
),
]);
}
Then, handle possible actions in your push handler:
Future<dynamic> onPush(String name, RemoteMessage payload) {
final action = UNNotificationAction.getIdentifier(payload.data);
if (action == 'MEETING_INVITATION') {
// do something
}
return Future.value(true);
}
Note: if user clickes your notification while app is in the background, push will be delivered through onResume without actually waking up the app. Make sure your handling of given action is quick and error free, as execution time in for apps running in the background is very limited.
Check the example project for fully working code.
If you want to use firebase, but not firebase messaging, add this configuration entry in your Info.plist (to avoid MissingPluginException):
<key>flutter_apns.disable_firebase_core</key>
<false/>
If only care about apns - use flutter_apns_only plugin. It does not depend on firebase. To ensure no swizzling (which is needed by original plugin to disable firebase) takes place, add this configuration entry in your Info.plist:
<key>flutter_apns.disable_swizzling</key>
<true/>
swift
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NSLog("PUSH registration failed: \(error)")
}
}
objc
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
// Override point for customization after application launch.
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"%@", error);
}
@end
Setting up push notifications on iOS can be tricky since there is no way to permit Apple Push Notification Service (APNS) which requires a complicated certificate setup. The following guide describes a step by step approach to send push notifications from your Mac to an iPhone utilizing the example app of this package. This guide only describes debug environment setup.
openssl pkcs12 -in flutterApns.p12 -out flutterApns.pem -nodes -clcerts
curl -v \
-d '{"aps":{"alert":"<your_message>","badge":2}}' \
-H "apns-topic: <bundle_identifier_of_registered_app>" \
-H "apns-priority: 10" \
--http2 \
--cert <file_path_to_downloaded_signed_and_converted_certificate>.pem \
https://api.development.push.apple.com/3/device/<device_token>
When not utilizing the example app, you need to additionally setup push notification capability inside Xcode and add the code mentioned in usage.