pushy / pushy-flutter

The official Pushy SDK for Flutter apps.
Apache License 2.0
21 stars 19 forks source link

Cannot receive Push Notifications after Update/Upgrade #16

Closed rgodlontonshaw closed 4 years ago

rgodlontonshaw commented 4 years ago

Hi there,

Please could you assist me. I have upgraded my Flutter SDK and Pushy library (pushy_flutter: ^1.1.2) recently and now I no longer receive any push notifications. I am using the flutter pushy demo code exactly.

I see this error in my logs. I/flutter (18015): Pushy: Error retrieving handle for Flutter callbacks

I/flutter (18015): null
D/Pushy   (18015): PushyJobService: Sending keep alive
D/InputMethodManager(18015): prepareNavigationBarInfo() DecorView@3954443[MainActivity]
D/InputMethodManager(18015): getNavigationBarColor() -855310
D/ViewRootImpl@898714a[MainActivity](18015): MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager(18015): prepareNavigationBarInfo() DecorView@3954443[MainActivity]
D/InputMethodManager(18015): getNavigationBarColor() -855310
D/SurfaceView(18015): onWindowVisibilityChanged(8) false io.flutter.embedding.android.FlutterSurfaceView{4a8f8bb V.E...... ........ 0,0-1080,2094} of ViewRootImpl@898714a[MainActivity]
D/OpenGLRenderer(18015): eglDestroySurface = 0xe6f8d8e0, 0xcb413800
D/ViewRootImpl@898714a[MainActivity](18015): Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x5 surface={false 0} changed=false
D/Pushy   (18015): PushyJobService: Sending keep alive

I have tried everything I possibly can think of. The notifications were working before on iOS and Android. Any advice/guidance/assistance would be much appreciated.

Flutter doctor:

[✓] Flutter (Channel stable, 1.20.4, on Mac OS X 10.15.7 19H2, locale en-ZA)
    • Flutter version 1.20.4 at /Users/ryangodlonton-shaw/Documents/development/tools/flutter
    • Framework revision fba99f6cf9 (2 weeks ago), 2020-09-14 15:32:52 -0700
    • Engine revision d1bc06f032
    • Dart version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/ryangodlonton-shaw/Library/Android/sdk
    • Platform android-30, build-tools 30.0.1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.0, Build version 12A7209
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

• No issues found!
pushy commented 4 years ago

Hi @rgodlontonshaw, It appears your project has been updated to version 1.1.2 of the pushy_flutter Dart package. This new version invokes your notification listener in the background, even if your app is not running, allowing you to generate a notification from Dart code. It requires a slight code change.

To update your project to the new Pushy Flutter SDK:

1) Please ensure your Flutter Android project has been updated to Flutter 1.12+ for compatibility with the new version of our plugin. Your android/app/src/main/AndroidManifest.xml should contain a <meta-data> attribute labeled flutterEmbedding with a value of 2.

2) Remove the following lines from your android/app/src/main/AndroidManifest.xml:

<!-- Pushy Notification Receiver -->
<!-- Incoming push notifications will invoke the following BroadcastReceiver -->
<receiver android:name="me.pushy.sdk.flutter.PushReceiver" android:exported="false">
    <intent-filter>
        <!-- Do not modify this -->
        <action android:name="pushy.me" />
    </intent-filter>
</receiver>

3) Remove any existing Pushy.setNotificationListener((data) => {}) method from your application.

4) Add the following method to your main.dart file, right after the import statements, and outside any Widget class declaration, to process push notifications in the background via a Flutter background isolate:

// Please place this code in main.dart,
// After the import statements, and outside any Widget class (top-level)

void backgroundNotificationListener(Map<String, dynamic> data) {
    // Print notification payload data
    print('Received notification: $data');

    // Notification title
    String notificationTitle = 'MyApp';

    // Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
    String notificationText = data['message'] ?? 'Hello World!';

    // Android: Displays a system notification
    // iOS: Displays an alert dialog
    Pushy.notify(notificationTitle, notificationText, data);

    // Clear iOS app badge number
    Pushy.clearBadge();
}

Feel free to modify this sample code to suit your own needs.

5) Call the Pushy.setNotificationListener(backgroundNotificationListener) method after Pushy.listen() to configure the background notification listener:

// Listen for push notifications received
Pushy.setNotificationListener(backgroundNotificationListener);

Please let us know if it resolves the issue.

rgodlontonshaw commented 4 years ago

Thanks so much for your response @pushy-me.

I've implemented everything as above. I get this back in my console:

E/MethodChannel#me.pushy.sdk.flutter/methods(29989): Failed to handle method call
E/MethodChannel#me.pushy.sdk.flutter/methods(29989): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at me.pushy.sdk.flutter.PushyPlugin.setNotificationListener(PushyPlugin.java:377)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at me.pushy.sdk.flutter.PushyPlugin.onMethodCall(PushyPlugin.java:134)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at android.os.Looper.loop(Looper.java:181)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at android.app.ActivityThread.main(ActivityThread.java:7073)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#me.pushy.sdk.flutter/methods(29989):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
E/flutter (29989): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: PlatformException(error, java.lang.Integer cannot be cast to java.lang.Long, null)
pushy commented 4 years ago

Thanks for the bug report, it seems that some versions of the Flutter engine are providing callback handles as Integer. We've deployed a fix in 554de16 and published in 1.1.3.

To update your project to the new Pushy Flutter SDK:

1) Install version 1.1.3 of our Flutter SDK by editing the pubspec.yaml in the root directory of your project and adding the following under the dependencies section:

pushy_flutter: 1.1.3

Run flutter pub get to fetch the dependency. Then, run your app and check if the Dart notification listener is invoked in the background.

rgodlontonshaw commented 4 years ago

Thanks so much. @pushy-me I've upgraded to version 1.1.3

No errors come through now but I am still not getting in any notifications at all?

D/Pushy   (20875): Initializing FlutterBackgroundExecutor background isolate
D/NetworkManagementSocketTagger(20875): tagSocket(131) with statsTag=0xffffffff, statsUid=-1
D/Pushy   (20875): Isolate called notificationCallbackReady()
[{"id":14,"result":{"value":"android","type":"_extensionType","method":"ext.flutter.platformOverride"}}]
[{"id":16,"result":{"timeDilation":"1.0","type":"_extensionType","method":"ext.flutter.timeDilation"}}]
[{"id":17,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.debugPaint"}}]
[{"id":18,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.debugPaintBaselinesEnabled"}}]
[{"id":19,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.repaintRainbow"}}]
[{"id":20,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.showPerformanceOverlay"}}]
[{"id":21,"result":{"enabled":"true","type":"_extensionType","method":"ext.flutter.debugAllowBanner"}}]
[{"id":22,"result":{"enabled":"true","type":"_extensionType","method":"ext.flutter.debugAllowBanner"}}]
[{"id":23,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.inspector.structuredErrors"}}]
[{"id":24,"result":{"enabled":"true","type":"_extensionType","method":"ext.flutter.inspector.structuredErrors"}}]
[{"id":25,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.inspector.show"}}]
[{"id":26,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.inspector.trackRebuildDirtyWidgets"}}]
[{"id":27,"result":{"enabled":"false","type":"_extensionType","method":"ext.flutter.inspector.trackRepaintWidgets"}}]
[{"id":15,"result":{"value":"android","type":"_extensionType","method":"ext.flutter.platformOverride"}}]
D/Pushy   (20875): Connected successfully (sending keep alive every 300 seconds)
pushy commented 4 years ago

Hi @rgodlontonshaw, Can you please ensure you are sending notifications to the right device token?

Please check the logs for the following line:

D/Pushy   (20875): Device Token: ..........................

Then, try to send a notification to that device token on this page: https://pushy.me/docs/android/send-test-notification

rgodlontonshaw commented 4 years ago

Perfect! Thanks so much @pushy-me. It's working now. 👍