parse-community / Parse-SDK-Flutter

The Dart/Flutter SDK for Parse Platform

Home Page:https://parseplatform.org

Repository from Github https://github.comparse-community/Parse-SDK-FlutterRepository from Github https://github.comparse-community/Parse-SDK-Flutter

Push notification not working in iOS

shafeekghaseel opened this issue · comments

New Issue Checklist

Issue Description

I'm not receiving Parse pushes in iOS device. When I send push from Firebase console I'm receiving it. When I send push from Parse dashboard not receiving. The error shows BadDeviceToken.

Steps to reproduce

This is the AppDelegate.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)
}
}
Screenshot 2023-12-10 at 12 01 39 PM

Future main() async {
await initFirebase();
await initParse();
await registerNotification();
}

Future initFirebase() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
}

Future initParse() async {
await Parse().initialize(
parseAppId,
parseServerUrl,
clientKey: parseClientKey,
debug: false,
);
}

push_config.dart

import 'dart:convert';
import 'dart:math';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:parse_server_sdk_flutter/parse_server_sdk_flutter.dart';

const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'This channel is used for important notifications.', // description
importance: Importance.max,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

Future registerNotification() async {
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);

if (settings.authorizationStatus == AuthorizationStatus.authorized) {
debugPrint('User granted permission');
} else if (settings.authorizationStatus == AuthorizationStatus.provisional) {
debugPrint('User granted provisional permission');
} else {
debugPrint('User declined or has not accepted permission');
return;
}

await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);

FirebaseMessaging.instance.onTokenRefresh.listen((value) async {
final installation = await ParseInstallation.currentInstallation();
installation.deviceToken = value;
await installation.save();
});

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);

ParsePush.instance.initialize(
FirebaseMessaging.instance,
parseNotification: ParseNotification(
onShowNotification: (message) {
flutterLocalNotificationsPlugin.show(
Random().nextInt(999999),
"",
message,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: '@drawable/push_icon',
// other properties...
),
),
);
},
),
);

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
showNotification(message);
});

FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
}

void showNotification(RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;

// If onMessage is triggered with a notification, construct our own
// local notification to show to users using the created channel.
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: android.smallIcon,
// other properties...
),
),
);
} else {
String dataString = message.data["data"] ?? "";
Map<String, dynamic>? data;
try {
data = JsonCodec().decode(dataString);
} catch (_) {}
if (data != null && data.containsKey('alert')) {
flutterLocalNotificationsPlugin.show(
Random().nextInt(999999),
"",
data['alert'],
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
icon: '@drawable/push_icon',
// other properties...
),
),
);
}
}
}

@pragma('vm:entry-point')
Future onBackgroundMessage(RemoteMessage message) async {
showNotification(message);
}

Actual Outcome

Sending from 'https://fcm.googleapis.com/fcm/send' this api to specific deviceToken receiving the push, From parse(Sashido) dashboard or using Parse.Push from application is not receiving push in iOS.

Expected Outcome

To Receive push notification in iOS.

Environment

Parse Flutter SDK

  • SDK version: 7.0.0
  • Flutter version: 3.16.2
  • Dart version: 3.2.2
  • Operating system version: iOS

Server

  • Parse Server version: 3.6.0

Logs

Thanks for opening this issue!

  • 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.

@shafeekghaseel

Is the problem solved?

Yes, I was storing fcm token instead of apns token.

On Wed, 13 Dec 2023 at 09:24, Mohammad Bagher Fakouri @.> wrote: @shafeekghaseel Is the problem solved? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.>

Excellent, thank you for the information