How to implement push notifications using Xam.Plugin.PushNotifications plugin

RemedinetRemedinet INMember ✭✭

Hi,

I implemented push notification using plugin ("xam.plugin.pushnotification") with Google Cloud Messaging(GCM). It's working fine when app is open. When I close the app not receiving GCM messages.

When I search for the solution they are asking to Add Application class and define StartPushService (). After Adding the class my android project throws below error when compile.

Error:
C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(3,3): Error MSB4018: The "GenerateJavaStubs" task failed unexpectedly.
System.InvalidOperationException: There can be only one type with an [Application] attribute; found: ,
at Xamarin.Android.Tasks.ManifestDocument.CreateApplicationElement(XElement manifest, List1 subclasses, List1 selectedWhitelistAssemblies)
at Xamarin.Android.Tasks.ManifestDocument.Merge(List1 subclasses, List1 selectedWhitelistAssemblies, Boolean embed, Boolean replaceSplashScreen, IDictionary2 splashScreenClasses, String bundledWearApplicationName, IEnumerable1 mergedManifestDocuments)
at Xamarin.Android.Tasks.GenerateJavaStubs.Run()
at Xamarin.Android.Tasks.GenerateJavaStubs.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__20.MoveNext() (MSB4018) (Preauth.Droid)

Application Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Runtime;
using PushNotification.Plugin;

namespace Preauth.Droid
{

[Application]
public class ProviderStart : Application
{
    public static Context AppContext;

    public ProviderStart (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer)
    {

    }

    public override void OnCreate ()
    {
        base.OnCreate ();

        AppContext = this.ApplicationContext;

        //TODO: Initialize CrossPushNotification Plugin
        //TODO: Replace string parameter with your Android SENDER ID
        //TODO: Specify the listener class implementing IPushNotificationListener interface in the Initialize generic
        CrossPushNotification.Initialize<CrossPushNotificationListener> ("My google Sender ID");

        StartPushService ();
    }

    public static void StartPushService ()
    {
        AppContext.StartService (new Intent (AppContext, typeof(PushNotificationService)));

        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) {

            PendingIntent pintent = PendingIntent.GetService (AppContext, 0, new Intent (AppContext, typeof(PushNotificationService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService (Context.AlarmService);
            alarm.Cancel (pintent);
        }
    }

    public static void StopPushService ()
    {
        AppContext.StopService (new Intent (AppContext, typeof(PushNotificationService)));
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) {
            PendingIntent pintent = PendingIntent.GetService (AppContext, 0, new Intent (AppContext, typeof(PushNotificationService)), 0);
            AlarmManager alarm = (AlarmManager)AppContext.GetSystemService (Context.AlarmService);
            alarm.Cancel (pintent);
        }
    }

}

}

could any one tell me how to solve the issue.

Answers

  • tanujtanuj USMember

    have you found the solution ?

  • MiguelCervantesMiguelCervantes MXMember ✭✭✭

    You have another [Application] attribute in your Android code.

  • RemedinetRemedinet INMember ✭✭

    yes i solved this issue. Once i upgrade my xamarin.form i got a file called "MainApplication.cs" in my project. There i added below code and it got worked.

    public override void OnCreate ()
    {
    base.OnCreate ();

        AppContext = this.ApplicationContext;
    
        //TODO: Initialize CrossPushNotification Plugin
        //TODO: Replace string parameter with your Android SENDER ID
        //TODO: Specify the listener class implementing IPushNotificationListener interface in the Initialize generic
        CrossPushNotification.Initialize<CrossPushNotificationListener> ("My google Sender ID");
    CrossPushNotification.Current.Register ();
        StartPushService ();
    }
    
    public static void StartPushService ()
        {
            AppContext.StartService (new Intent (AppContext, typeof(PushNotificationService)));
    
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) {
    
                PendingIntent pintent = PendingIntent.GetService (AppContext, 0, new Intent (AppContext, typeof(PushNotificationService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService (Context.AlarmService);
                alarm.Cancel (pintent);
            }
        }
    
        public static void StopPushService ()
        {
            AppContext.StopService (new Intent (AppContext, typeof(PushNotificationService)));
            if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) {
                PendingIntent pintent = PendingIntent.GetService (AppContext, 0, new Intent (AppContext, typeof(PushNotificationService)), 0);
                AlarmManager alarm = (AlarmManager)AppContext.GetSystemService (Context.AlarmService);
                alarm.Cancel (pintent);
            }
        }
    
  • ShahriatHossainShahriatHossain USMember ✭✭

    @Remedinet hi, I am having problem integrating this plugin, I have followed their tutorial https://github.com/rdelrosario/xamarin-plugins/tree/master/PushNotification but still didn't get any notification on my real device. I am trying to integrate this with pubnub client but no luck both for android and ios, can you help me?

  • Scott_WolfScott_Wolf INMember ✭✭

    @Remedinet Sorry to Tag you in.

    Did you able to implement IPushnotification inside PCL project? Or you implemented it in every platform?

    I've posted my question in here:
    https://forums.xamarin.com/discussion/85324/cant-access-api-classes-ipushnotification-from-xam-plugin-pushnotification-in-xf-pcl-project/p1?new=1

  • AFernandhinoAFernandhino USMember

    Remedinet
    To which version did you update the Xamarin.Forms?

  • sumitsisodia.9614sumitsisodia.9614 USMember ✭✭

    i create Push-notification in xamarin form and i use Xam.Plugin.PushNotifications plugin
    but im getting this error:-
    Java.Lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/3511/501e63ce/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
    at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
    at Android.Gms.Gcm.Iid.InstanceID.GetInstance (Android.Content.Context context) [0x00049] in :0
    at PushNotification.Plugin.PushNotificationRegistrationIntentService.OnHandleIntent (Android.Content.Intent intent) [0x00018] in C:\xamarin-plugins\PushNotification\PushNotification\PushNotification.Plugin.Android\PushNotificationRegistrationIntentService.cs:35
    --- End of managed Java.Lang.IncompatibleClassChangeError stack trace ---
    java.lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat
    at com.google.android.gms.iid.zzd.zzde(Unknown Source)
    at com.google.android.gms.iid.zzd.(Unknown Source)
    at com.google.android.gms.iid.zzd.(Unknown Source)
    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
    at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.n_onHandleIntent(Native Method)
    at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.onHandleIntent(PushNotificationRegistrationIntentService.java:37)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.os.HandlerThread.run(HandlerThread.java:61)
    - Register - PushNotificationRegistationIntentService

    How to fix it????

  • sumitsisodia.9614sumitsisodia.9614 USMember ✭✭

    i create Pushnotification in xamarin form and i use
    but im getting this error Java.Lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/3511/501e63ce/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
    at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
    at Android.Gms.Gcm.Iid.InstanceID.GetInstance (Android.Content.Context context) [0x00049] in :0
    at PushNotification.Plugin.PushNotificationRegistrationIntentService.OnHandleIntent (Android.Content.Intent intent) [0x00018] in C:\xamarin-plugins\PushNotification\PushNotification\PushNotification.Plugin.Android\PushNotificationRegistrationIntentService.cs:35
    --- End of managed Java.Lang.IncompatibleClassChangeError stack trace ---
    java.lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat
    at com.google.android.gms.iid.zzd.zzde(Unknown Source)
    at com.google.android.gms.iid.zzd.(Unknown Source)
    at com.google.android.gms.iid.zzd.(Unknown Source)
    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
    at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.n_onHandleIntent(Native Method)
    at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.onHandleIntent(PushNotificationRegistrationIntentService.java:37)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.os.HandlerThread.run(HandlerThread.java:61)
    - Register - PushNotificationRegistationIntentService

    How to fix it????

  • sumitsisodia.9614sumitsisodia.9614 USMember ✭✭

    i create Push-notification in xamarin form and i use Xam.Plugin.PushNotifications plugin
    but im getting this error:-
    Java.Lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/3511/501e63ce/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
    at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562
    at Android.Gms.Gcm.Iid.InstanceID.GetInstance (Android.Content.Context context) [0x00049] in :0
    at PushNotification.Plugin.PushNotificationRegistrationIntentService.OnHandleIntent (Android.Content.Intent intent) [0x00018] in C:\xamarin-plugins\PushNotification\PushNotification\PushNotification.Plugin.Android\PushNotificationRegistrationIntentService.cs:35
    --- End of managed Java.Lang.IncompatibleClassChangeError stack trace ---
    java.lang.IncompatibleClassChangeError: android.support.v4.content.ContextCompat
    at com.google.android.gms.iid.zzd.zzde(Unknown Source)
    at com.google.android.gms.iid.zzd.(Unknown Source)
    at com.google.android.gms.iid.zzd.(Unknown Source)
    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
    at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.n_onHandleIntent(Native Method)
    at md563bb5d7cd114ceda82ec85fb57ce3637.PushNotificationRegistrationIntentService.onHandleIntent(PushNotificationRegistrationIntentService.java:37)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.os.HandlerThread.run(HandlerThread.java:61)
    - Register - PushNotificationRegistationIntentService

    How to fix it????

  • HomeroLaraHomeroLara USUniversity, Developer Group Leader ✭✭

    Hi sumitsisodia.9614.

    Are you compiling your Android app against API 21? I believe the github documentation (https://github.com/rdelrosario/xamarin-plugins/tree/master/PushNotification) states your app should be compiled against api 21.

    Best!

  • pnet1pnet1 USMember ✭✭✭✭

    I had this error and in my experience was because i didn't put in Constructor of MainActivity this line:

    AppContext = this.ApplicationContext;

    but this in my experience. Maybe for you is other thing.

  • aeodeveloperaeodeveloper Member ✭✭

    i got error "CrossPushNotification doesnt contain a defenition for Initialize" on

    CrossPushNotification.Initialize("My google Sender ID");
    can someone help me :neutral:

Sign In or Register to comment.