MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.47k stars 1.4k forks source link

Can't get the notification #2289

Closed Nabeel-Shehzad closed 7 months ago

Nabeel-Shehzad commented 7 months ago

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 commented 7 months ago

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