Comments (4)
Sorry I should have added some more info:
I followed your instructions in PushNotificationStarted.txt to start the service.
And my app is a Xamarin Form app, here is a code extraction for the initialization (onCreate):
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
public static Context AppContext;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
AppContext = this.ApplicationContext;
global::Xamarin.Forms.Forms.Init (this, bundle);
//Sender Id pour com.vetup.imrvot => 845213152730
CrossPushNotification.Initialize<CrossPushNotificationListener>("845213152730");
StartPushService();
LoadApplication (new App ());
}
from xamarin-plugins.
You need to do this if you wish to run it even when app closed. Instead of initializing the plugin in the MainActivity.cs. Do the following:
Implement an Android Application class in your Droid project and initialize plugin there. Here a brief snippet:
public class YourAndroidApplication : Application
{
public static Context AppContext;
public YourAndroidApplication(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>("<ANDROID SENDER ID>");
//This service will keep your app receiving push even when closed. 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);
}
}
Just move your initialization stuff from MainActivity.cs to this Android Application class. Also Replace YourAndroidApplication name to your App name.
Hope this helps. Let me know if need any additional help.
from xamarin-plugins.
Thanks a lot for your help, it worked,
You did explained this in your settup guide but I didn't understand it was also appropriate in the contexte of a Xamarin Form app.
Besides, I had to add the Application attribute to make it work:
[Application]
public class VOT : Android.App.Application
{
public static Context AppContext;
public VOT (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>("845213152730");
//This service will keep your app receiving push even when closed.
StartPushService();
}
from xamarin-plugins.
Please help me to solve the same case in my application. I am also following same case that you have done in your project for push notifications functionality.
I was getting push notifications when the app is running. But when the app was closed then app not able to getting push notifications.
I have created the Android Application class that you have followed in your scenario, but I was getting below error.
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Error executing task GenerateJavaStubs: There can be only one type with an [Application] attribute; found: , (MyApplicationName.Droid).
Please suggest me, how can I solve this problem in my application. And how can I get the push notifications even app is closed.
If you have any sample code for push notifications functionality in Xamarin.Forms, Please share with me.
Thanks, Hima
from xamarin-plugins.
Related Issues (20)
- PushNotifications: Empty Body with FCM on Android HOT 1
- Help configuring custom icon and sound on notification HOT 2
- How do I get the Accelerometer and Magenetometer values simultaniously?
- got an error while registering push notifications HOT 4
- CrossPushNotificationListener Missing
- UWP notifications not firing HOT 3
- ANDROID Crash INVALID_PARAMETERS HOT 1
- MotionVector.cs ToString implementation
- Android - calling register() causes nav bar to disappear. HOT 1
- How can i convert the ((MotionVector)a.Value).Z from accelerattor to a angle? HOT 1
- Edit push notification sound
- Reconvert iOS Device Token to NsData problem
- Problem receiving push in iOS 11 HOT 3
- Tizen support
- Android 8 not getting notifications HOT 1
- Light Sensor
- Support .Net standard 2.0
- device motion, SensorValueChanged not fired
- Sample App is not working
- All plugins are deprecated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xamarin-plugins.