OneSignal / OneSignal-Xamarin-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Xamarin app with OneSignal. https://onesignal.com
Other
104 stars 50 forks source link

Unable to instantiate receiver com.onesignal.GcmBroadcastReceiver when using R8 code shrinker (com.onesignal.FCMBroadcastReceiver when using OneSignal 4.0.0) #235

Open Hakim83 opened 3 years ago

Hakim83 commented 3 years ago

Description: I followed the documentation to make Xamarin.Forms OneSignal notification app targeting Android. In release mode, it works well, I receive push notification message (I tried it using tags), but if I recompile with Android code shrinker set to 'r8' the app crashes when receiving notification. following is the android device monitor log:

04-17 10:22:35.468: E/AndroidRuntime(16674): java.lang.RuntimeException: Unable to instantiate receiver com.onesignal.GcmBroadcastReceiver: java.lang.ClassNotFoundException: Didn't find class "com.onesignal.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.myonesignalapp-Nk9WnS6KwJ6M4tnRd4FiwA==/base.apk"],nativeLibraryDirectories=[/data/app/com.myonesignalapp-Nk9WnS6KwJ6M4tnRd4FiwA==/lib/x86, /data/app/com.myonesignalapp-Nk9WnS6KwJ6M4tnRd4FiwA==/base.apk!/lib/x86, /system/lib]]
04-17 10:22:35.468: E/AndroidRuntime(16674): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.onesignal.GcmBroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.myonesignalapp-Nk9WnS6KwJ6M4tnRd4FiwA==/base.apk"],nativeLibraryDirectories=[/data/app/com.myonesignalapp-Nk9WnS6KwJ6M4tnRd4FiwA==/lib/x86, /data/app/com.myonesignalapp-Nk9WnS6KwJ6M4tnRd4FiwA==/base.apk!/lib/x86, /system/lib]]

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.myonesignalapp">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
    <permission android:name="${applicationId}.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application android:label="OneSignalTest.Android" android:theme="@style/MainTheme">
        <receiver android:name="com.onesignal.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="${applicationId}" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Environment Visual Studio Community 2019 version 16.9.2 Xamarin.Android SDK 11.2.2.1 Com.OneSignal Nuget version 3.10.5

Steps to Reproduce Issue:

  1. Create new Xamarin.Forms project
  2. setup it as described in documentation for app.xaml.cs file and android project androidmanifest.xml
  3. register notification in your code
  4. in release mode android settings set code shrinker to r8
  5. run the app and try to send notification from backend
tanaynigam commented 2 years ago

@Hakim83 Thanks for reporting the issue. We have a release OneSignalSDK.Xamarin-4.0.0 in which this issue no longer seems to exist. Please note that we have changed the package name and namespace for the SDK from Com.OneSignal to OneSignalSDK.Xamarin and would require you to remove the previous package name and add the new package name to import the SDK in your code.

Additionally, when searching for the package on nuget.org, you will be required to search for OneSignalSDK.Xamarin to import the package

dush135 commented 2 years ago

Problem still exits. If we use r8 shrinking in Android we are facing this issue. Only way to get away with this crash is adding a custom proguard.cfg file and add following lines to it

-dontobfuscate

-keep class com.onesignal.FCMBroadcastReceiver
jkasten2 commented 2 years ago

@dush135 Could you let us know what Android versions and device models you are seeing this issue on?

dush135 commented 2 years ago

Tested devices

Samsung s10+ = Android 12 Samsung A51 = Android 12 Sony ZS1 = Android 9

Tested on both MAC and Windows Visual Studio 2022

Xamarin.Forms 5.0.0.2401 OneSignalSDK.Xamarin-4.0.0

it11111111 commented 1 year ago

This is still an issue for me as well. Using Visual Studio 2022 - OneSIgnalXamarin SDK

Using R8 and release mode.

Tested devices: Samsung s20 = Android 12 Samsung S21fe = Android 12 Samsung S20 Ultra = Android 12

Xamarin.Forms 5.0.0.2401 OneSignalSDK.Xamarin-4.1.3

jkasten2 commented 1 year ago

Original Issue - com.onesignal.GcmBroadcastReceiver

For those seeing this with com.onesignal.GcmBroadcastReceiver when using OneSignal-Xamarin-SDK 4.0.0 or newer, make sure you have removed all packages with Com.OneSignal and replaced them with OneSignalSDK.Xamarin. com.onesignal.GcmBroadcastReceiver was removed and replaced with com.onesignal.FCMBroadcastReceiver in this version. If you still see the error about GcmBroadcastReceiver when using OneSignal 4.0.0 then I recommend you clean your project in Visual Studio, as well as manually removed the bin and obj folders in your project.

com.onesignal.FCMBroadcastReceiver

For those seeing this with com.onesignal.FCMBroadcastReceiver, I am surprised this isn't correctly picked up by Visual Studio as FCMBroadcastReceiver is defined in OneSignal's AndroidManifest.xml. This seems like a bug with Visual Studio as native apps nor other frameworks like Flutter have this issue.

The next step here would be to see if the issue still happens with a MAUI project. Either way, it could be reported to Microsoft to see if they can address it in a future update.

van8tka commented 5 months ago

OneSignalSDK.DotNet v 5.10 Net-8-android removed <receiver android:name="com.onesignal.FCMBroadcastReceiver"... from AndroidManifest.xml - its' work for me