ios – APNS Token get null


import ‘dart:typed_data’;

import ‘package:flutter/material.dart’;
import ‘dart:io’;
import ‘package:firebase_messaging/firebase_messaging.dart’;
import ‘package:flutter_local_notifications/flutter_local_notifications.dart’;
import ‘package:fluttertoast/fluttertoast.dart’;

import ‘package:pump_pipe/amc_quotation.dart’;

import ‘package:pump_pipe/draw_screen.dart’;
import ‘package:pump_pipe/home_screen.dart’;
import ‘package:pump_pipe/repair_detail.dart’;
import ‘package:pump_pipe/wallet_screen.dart’;
import ‘package:pump_pipe/your_quotation.dart’;
import ‘package:shared_preferences/shared_preferences.dart’;

import ‘../main.dart’;
import ‘../your_orders.dart’;

Future<void> handlebackgroundMessage(RemoteMessage message) async {
final notification = message.notification;
print(‘Title1: ${message.notification?.title}’);
print(‘body: ${message.notification?.body}’);
print(‘payload: ${message.data}’);
_showLocalNotification(notification!);
_navigateToScreen(notificationType);
}

final _locaNotifications = FlutterLocalNotificationsPlugin();
Future<void> handleForegroundMessage(RemoteMessage message) async {
final notification = message.notification;
print(‘Title: ${message.notification?.title}’);
print(‘body: ${message.notification?.body}’);
print(‘payload: ${message.data}’);
_showLocalNotification(notification!);
String notificationType = message.notification?.android?.clickAction ?? ”;
_navigateToScreen(notificationType);
}

String notificationType = “”;

void handleMessage(RemoteMessage? message) {
if (message == null) return;
final notification = message.notification;
final data = message.data;

if (notification != null) {

}
}

Future initPushNotification() async {
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
FirebaseMessaging.onBackgroundMessage(handlebackgroundMessage);

FirebaseMessaging.onBackgroundMessage((message) async {
print(“Handling background message: $message”);
handlebackgroundMessage(message);
String notificationType = message.notification?.android?.clickAction ??
”;

_navigateToScreen(notificationType);

});

FirebaseMessaging.instance.getInitialMessage().then(handleMessage);
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {

handleMessage(message);

String notificationType = message.notification?.android?.clickAction ??
”;
_navigateToScreen(notificationType);
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
final notification = message.notification;
if (notification == null) return;
_showLocalNotification(notification);

print(‘Title: ${notification.title}’);
print(‘body: ${notification.body}’);
print(‘payload: ${message.data}’);

});
}

void _showLocalNotification(RemoteNotification notification) async {
await _locaNotifications.show(
0,
notification.title ?? ”,
notification.body ?? ”,
NotificationDetails(
android: AndroidNotificationDetails(
‘high_importance_channel’,
‘High Importance Notifications’,
importance: Importance.max,
priority: Priority.high,
additionalFlags: Int32List.fromList(<int>[4]),
),
),
payload: notification.android?.clickAction ?? ”,
);
notificationType = notification.android?.clickAction ??
”;
}

class FirebaseApi {
final _firebaseMessaging = FirebaseMessaging.instance;

final _androidchannel = const AndroidNotificationChannel(
‘high_importance_channel’,
‘High Importance Notifications’,
description: ‘This Channel is used for important notification’,
importance: Importance.defaultImportance,
);

final _locaNotifications = FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
await _firebaseMessaging.requestPermission();
SharedPreferences _pref = await SharedPreferences.getInstance();
if (Platform.isAndroid) {
final fCMToken = await _firebaseMessaging.getToken();
print(“fcm”);
_pref.setString(‘fcm’, fCMToken!);

else if (Platform.isIOS) {
NotificationSettings settings =
await _firebaseMessaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);

if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print(‘User granted permission’);

await _firebaseMessaging.getAPNSToken().then((value) async {
var iosToken = value ?? ”;
Fluttertoast.showToast(msg: “token $iosToken”);
_pref.setString(‘fcm’, iosToken);
print(‘Token 11212 : $iosToken’);
});
else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
print(‘User granted provisional permission’);
else {
print(‘User declined or has not accepted permission’);
}
}

await _locaNotifications.initialize(
InitializationSettings(
android: AndroidInitializationSettings(
‘@mipmap/ic_launcher’),
iOS: DarwinInitializationSettings(
requestSoundPermission: true,
requestAlertPermission: true,
)),
onDidReceiveNotificationResponse: (message) async {

print(‘User tapped on the notification’);

_navigateToScreen(notificationType);
},
);`

await _locaNotifications
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(_androidchannel);

initPushNotification();
}
}

}

i get APNS Token but Always fetch Null , how to get APNS token , add info.plist in my project and all firebase set up done

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img