Skip to content
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

Can't get the notification #2289

Closed
Nabeel-Shehzad opened this issue Apr 2, 2024 · 1 comment
Closed

Can't get the notification #2289

Nabeel-Shehzad opened this issue Apr 2, 2024 · 1 comment

Comments

@Nabeel-Shehzad
Copy link

Describe the bug
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:463)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:364)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1248)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1588)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:1413)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at android.os.Handler.handleCallback(Handler.java:958)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at android.os.Looper.loopOnce(Looper.java:205)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at android.os.Looper.loop(Looper.java:294)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at android.app.ActivityThread.main(ActivityThread.java:8177)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/MethodChannel#dexterous.com/flutter/local_notifications(16232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/flutter (16232): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/flutter (16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:463)
E/flutter (16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:364)
E/flutter (16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1248)
E/flutter (16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1588)
E/flutter (16232): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:1413)
E/flutter (16232): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/flutter (16232): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/flutter (16232): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/flutter (16232): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (16232): at android.os.Handler.handleCallback(Handler.java:958)
E/flutter (16232): at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (16232): at android.os.Looper.loopOnce(Looper.java:205)
E/flutter (16232): at android.os.Looper.loop(Looper.java:294)
E/flutter (16232): at android.app.ActivityThread.main(ActivityThread.java:8177)
E/flutter (16232): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (16232): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/flutter (16232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/flutter (16232): )
E/flutter (16232): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
E/flutter (16232): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
E/flutter (16232):
E/flutter (16232): #2 FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:240:7)
E/flutter (16232):

To Reproduce

  1. Go to firebase cloud messaging
  2. Click on send test message

Sample code to reproduce the problem
import 'dart:math';

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

class NotificationServices {
FirebaseMessaging messaging = FirebaseMessaging.instance;
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

void requestNotificationPermission() async {
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
} else if (settings.authorizationStatus ==
AuthorizationStatus.provisional) {
print('User granted provisional permission');
} else {
AppSettings.openAppSettings(
type: AppSettingsType.notification,
);
}
}

void initLocalNotifications(
BuildContext context, RemoteMessage message) async {
var androidInit = AndroidInitializationSettings('@drawable/ic_launcher');
var iosInit = DarwinInitializationSettings();

var initSettings = InitializationSettings(
  android: androidInit,
  iOS: iosInit,
);

await _flutterLocalNotificationsPlugin.initialize(initSettings,
    onDidReceiveNotificationResponse: (payload) {});

}

void firebaseInit() {
FirebaseMessaging.onMessage.listen((message) {
showNotification(message);
});
}

Future showNotification(RemoteMessage message) async {
AndroidNotificationChannel channel = AndroidNotificationChannel(
Random.secure().nextInt(1000).toString(),
'High Importance Notifications',
description: 'This channel is used for important notifications',
importance: Importance.max,
playSound: true,
enableVibration: true,
);
var androidDetails = AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
importance: Importance.max,
playSound: true,
enableVibration: true,
ticker: 'ticker',
);

DarwinNotificationDetails iosDetails = DarwinNotificationDetails(
  presentAlert: true,
  presentBadge: true,
  presentSound: true,
);

var platformChannelSpecifics = NotificationDetails(
  android: androidDetails,
  iOS: iosDetails,
);

Future.delayed(Duration.zero, () {
  _flutterLocalNotificationsPlugin.show(
    0,
    message.notification!.title.toString(),
    message.notification!.body.toString(),
    platformChannelSpecifics,
  );
});

}

Future<String?> getDeviceToken() async {
return await messaging.getToken();
}

//if token refresh
void onTokenRefresh() async {
messaging.onTokenRefresh.listen((event) {
event.toString();
});
}
}

@Nabeel-Shehzad
Copy link
Author

added
icon: '@drawable/ic_launcher',
in AndroidNotificationDetails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant