bytefish / FcmSharp

Firebase Cloud Messaging (FCM) with .NET
MIT License
6 stars 7 forks source link

Add Firebase Messaging Examples (Notifications, Data Messages, ...) #34

Closed bytefish closed 5 years ago

bytefish commented 6 years ago

Currently there is a lack of examples for the library. I am presenting a Data Message in the README, which doesn't show what most users want to do: Notifications. I have already added the simple Android quickstart-messaging to the Examples folder.

The README should be updated and Examples with the Android example should be shown, especially for Notifications.

kehollin commented 5 years ago

Edit: Worked for Android after removing the topic element, but there was a significant lag (several minutes). Going to try setting priority now.

Edit 2: Worked (once) for iOS. Added the "apns-priority":"10" (immediate) header to the ApnsConfig, but didn't seem to speed it up at all. Thank you so much for an awesome library! I'll be back when things slow down a bit in a couple of weeks to see if I can create some more messages in the examples and send you a pull request.

Edit 3: Added ClickAction to AndroidNotification and Priority to AndroidConfig, forgot to add it in the first edit. The ClickAction seemed to be required?

I'd love to help out, but struggling to find sufficient documentation on the semantics of the fields. If the Firebase docs were better this would be easier. It's also confusing for me how the different app states (foreground, background, killed) play into it, so I'm not 100% comfortable I'm getting all the messages. I'll come back and share what I learn. In the meantime, if you by chance have any working notifications that could be included in the read.me quickly I would really appreciate it!

Here's what I'm going to try first:

                var pushMessage = new FcmMessage
                {
                    ValidateOnly = false,
                    Message = new Message
                    {
                        Token = tokenParm.pvalue,
                        Topic = DEFAULT_TOPIC,
                        Notification = new Notification
                        {
                            Title = $"{appName}: tasks due",
                            Body = $"You have {tasksDue} tasks due, please open the app now to catch up!",
                        },
                        AndroidConfig = new AndroidConfig
                        {
                            Notification = new AndroidNotification
                            {
                                Title = $"{appName}: tasks due",
                                Body = $"You have {tasksDue} tasks due, please open the app now to catch up!",
                                Sound = "default",
                                ClickAction = "OPEN_FROM_NOTIFICATION"
                            },
                            Priority = AndroidMessagePriorityEnum.HIGH
                        },
                        ApnsConfig = new ApnsConfig
                        {
                            Headers = new Dictionary<string, string>()
                            {
                                {"apns-priority", "10"}
                            },
                            Payload = new ApnsConfigPayload
                            {
                                Aps = new Aps
                                {
                                    Sound = "default",
                                    Badge = tasksDue
                                }
                            }
                        }
                    }
                };
bytefish commented 5 years ago

@kehollin Thanks for your efforts! The Firebase Docs are indeed a bit sparse on information, making it a little hard to build a rich set of samples. I will add an example for notifications later today!

kehollin commented 5 years ago

Thank you! I've updated the message initializer in my comment. I haven't testing rigorously (i.e. app in foreground, background and killed states) but will be doing so this coming week and will update the thread on GitHub if I find any problems. One thing to note is that I'm sending to a specific device ID, but including both AndroidConfig and ApnsConfig primarily because I'm too lazy to code for different platforms, but it seems to be allowed and harmless.

Kevin Hollingshead Research Software Engineer

Arizona State University College of Health Solutions School of Nutrition and Health Promotion 602-615-9972 (cell)

On Sat, Nov 17, 2018 at 10:01 PM Philipp Wagner notifications@github.com wrote:

@kehollin https://github.com/kehollin Thanks for your efforts! The Firebase Docs are indeed a bit sparse on information, making it a little hard to build a rich set of samples. I will add an example for notifications later today!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bytefish/FcmSharp/issues/34#issuecomment-439668073, or mute the thread https://github.com/notifications/unsubscribe-auth/AExGMenkjbGAbuPGf9KyQHOCloVn6xGEks5uwOm0gaJpZM4W1Hu7 .

bytefish commented 5 years ago

@kehollin Good job! 👍I would like to add (a slightly modified) example of your sample to the README, if you are OK with it? 😎

kehollin commented 5 years ago

Sure, maybe mask the device id too, no?

On Sat, Nov 17, 2018, 23:04 Philipp Wagner <notifications@github.com wrote:

@kehollin https://github.com/kehollin Good job! 👍I would like to add (a slightly modified) example of your sample to the README, if you are OK with it? 😎

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/bytefish/FcmSharp/issues/34#issuecomment-439670217, or mute the thread https://github.com/notifications/unsubscribe-auth/AExGMYuNM7C4l5fFoKCbTanp5r6y5XIAks5uwPhVgaJpZM4W1Hu7 .

bytefish commented 5 years ago

@kehollin I have added an example for Notifications now, along with an Android sample project. The old tutorial was much too complicated, this one is a better Quickstart. Along the way I have also simplified reading the Service Account Key in the library, so one doesn't need to hardcode the project key in Code anymore.

I have not integrated your example yet, but plan to do so. But I would need to write a longer documentation on it, because people will have questions I probably cannot answer... especially for the Apple iOS part, that I have not a lot of experience with.

If you find the time it would be great, if you could make a Pull Request to the README and put an example in the "Advanced" section. Maybe explaining all it in a Quickstart is too heave on Android / C# starters.

bytefish commented 5 years ago

Examples have been added for most of the Functions. The Android application makes a good start for Newcomers to Firebase and it is easy to get started with it. Everything else must be extracted from the docs, because I cannot make up examples for the entire API.