rdelrosario / xamarin-plugins

Cross platform Xamarin & Windows plugins for PCLs
MIT License
179 stars 161 forks source link

Example project for Push Notifications #7

Open JoshuaNovak919 opened 9 years ago

JoshuaNovak919 commented 9 years ago

Is there a way we can get a simple example project that uses the Push Notification plugin? The docs are a bit confusing and i'm not sure how i'm supposed to implement all the parts even after reading the txt files etc.

rdelrosario commented 9 years ago

Hi

There is instructions and step by step here:

http://lemoncode.net/2015/04/17/adding-push-notifications-to-your-xamarin-forms-application-part-1/

Let me know if helps.

pjdupreez commented 9 years ago

Hi there,

The link is broken. Cannot get to the instructions.

I did manage to get it working with the code on the main page, but how do I handle opening the notification? Notifications are received by my app and tapping the notification open my app, but on the first page. How can I tell it to open a specific page (I am using it for both iOS and Android in a Xamarin forms project)?

codebase2015 commented 8 years ago

Hi Rendy,

First of all I am really thankful that you have developed the plugin for Xamarin and AWS and helping developers across the globe.

I am new to mobile development and trying to develop a mobile app for andriod platform using xamrin. I do have requirement of sending notfications to indivisual devices through Amazon SNS services.
I need your help on getting the tutorial link for Xamarin Push Notification with Amazon SNS. http://lemoncode.net/2015/04/17/adding-push-notifications-to-your-xamarin-forms-application-part-1/ (this is broken link) Could you please provide me the full example or link which I can follow.
It would be a great help. Thank you Ashish

homerokzam commented 8 years ago

I would also like to set an example for Android.

rdelrosario commented 8 years ago

Try the link of Step by step instructions on the README documentation just updated the readme. Shouldn't be broken know let me know if works. On Jul 23, 2015 10:41 AM, "homerokzam" notifications@github.com wrote:

I would also like to set an example for Android.

— Reply to this email directly or view it on GitHub https://github.com/rdelrosario/xamarin-plugins/issues/7#issuecomment-124128269 .

espilacopa commented 8 years ago

My bad the link in the readme work just fine !! you're a boss !

thanks again !!

Hello, thanks for all your work but the link is still broken ! if you can do something !

jwood803 commented 8 years ago

I think this is the updated link from above.

Though, I'm still confused on how to implement in Android to receive the device token and when a message is received. Looking at part two from the link, it still doesn't seem to show that.

Aybukes commented 7 years ago

This is the example link https://lemoncoders.wordpress.com/2015/04/17/adding-push-notifications-to-your-xamarin-forms-application-part-1/

nmdias commented 7 years ago

Does anyone have a working example? I'm not succeeding in implementing this. None of the above links are working.

Thank you.

CR4567 commented 7 years ago

If you could be more specific we could help you. In the readme you can find the normal way to get it working, except you have special requirements.

nmdias commented 7 years ago

No special requirements, but I have to be missing something.

I have implemented the IPushNotificationListener, in my CrossPushNotificationListener class. As suggested in the README file.

public class CrossPushNotificationListener : IPushNotificationListener
{
    void IPushNotificationListener.OnError(string message, DeviceType deviceType)
    {
        Application.Current.MainPage.DisplayAlert("error", message, "ok");
    }

    void IPushNotificationListener.OnMessage(JObject values, DeviceType deviceType)
    {
        Application.Current.MainPage.DisplayAlert("message", values.ToString(), "ok");
    }

    void IPushNotificationListener.OnRegistered(string token, DeviceType deviceType)
    {
        Application.Current.MainPage.DisplayAlert("token", token, "ok");
    }

    void IPushNotificationListener.OnUnregistered(DeviceType deviceType)
    {
        Application.Current.MainPage.DisplayAlert("unregistered", "", "ok");
    }

    bool IPushNotificationListener.ShouldShowNotification()
    {
        Application.Current.MainPage.DisplayAlert("should show notification", "", "ok");
        return true;
    }
}

In iOS’s AppDelegate, I then initialize the CrossPushNotification plugin.

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init ();

    CrossPushNotification.Initialize<CrossPushNotificationListener>();

    LoadApplication(new Origination.App ());
    return base.FinishedLaunching(app, options);
}

I have also extended the AppDelegate with the appropriate overrides as shown in the PushNotificationApplicationDelegate.txt.pp file:

public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
    if (CrossPushNotification.Current is IPushNotificationHandler)
    {
        ((IPushNotificationHandler)CrossPushNotification.Current).OnErrorReceived(error);
    }
}

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    if (CrossPushNotification.Current is IPushNotificationHandler)
    {
        ((IPushNotificationHandler)CrossPushNotification.Current).OnRegisteredSuccess(deviceToken);
    }
}

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    if (CrossPushNotification.Current is IPushNotificationHandler)
    {
        ((IPushNotificationHandler)CrossPushNotification.Current).OnMessageReceived(userInfo);
    }
}

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
    if (CrossPushNotification.Current is IPushNotificationHandler)
    {
        ((IPushNotificationHandler)CrossPushNotification.Current).OnMessageReceived(userInfo);
    }
}

Afterwards, in my shared code, after the user registered/logged in the App and enter it's home screen, I call:

CrossPushNotification.Current.Register();

I know this method is being executed, as I get an alert requesting permissions. But none of the methods from the IPushNotificationListener interface implemented in the CrossPushNotificationListener are called.

There must be something I'm not seeing here..

CR4567 commented 7 years ago

Did you install it on your real device or simulator? Are you sure that a push notification is sent to your device? Are using Azure or a different Push Server?

nmdias commented 7 years ago

I'm testing this on a real device.

My issue is that, the CrossPushNotification.Current.Register() method, while it's triggering an alert to request permissions for push notifications (as expected), the implementation of the IPushNotificationListener interface is never called. So I never get the device's token.

In fact, none of the method's from the AppDelegate are called (that's weird, considering the permissions alert).

CR4567 commented 7 years ago

Ok, I see. You're right, at least the Register callbacks of AppDelegate should be triggered. But what I've seen looks good to me... Could you give us access to your code? Or maybe someone else has an idea

gtmorais commented 7 years ago

Same problem as nmdias, it does request permission for push notifications and calls DidRegisterUserNotificationSettings but never calls RegisteredForRemoteNotifications in order to get the device's token. For Android I've updated the payload fields that comes from Firebase on the plugin's source and now it shows the notification body on Android. But not for IOS. My CrossPushNotificationListener is on the PCL, only working for Android so far.

gtmorais commented 7 years ago

Now it calls RegisteredForRemoteNotifications but when sending push it says the token is invalid using Pusher. So the token is invalid for now. It may be IOS 10 issue with this plugin.

nmdias commented 7 years ago

@gtmorais can you try adding a group to your app's entitlements? Give that a go.

I honestly don't remember if that was it, as I ended up with a custom dependency service.

Here's what I did, based on the plugin's source code:

http://stackoverflow.com/a/42422790/1544047

pjavax commented 6 years ago

My project do not work when i register CrossPushNotification in the OnStart method. I no have a error message, but don't run. When i debug it, then get in the Service to consume, don't work. Any help is welcome.