Exemplo n.º 1
0
        /// <summary>
        /// Called when registration state changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PushModel_StateChanged(object sender, PushConnectionStateEventArgs e)
        {
            Log.Debug(Tag, $"Push State: [{e.State}], Error String: [{e.Error}]");

            State newState = State.Unregistered;

            if (e.State == PushConnectionStateEventArgs.PushState.Registered)
            {
                newState = State.Registered;

                // Get the registration id
                string id = PushClient.GetRegistrationId();
                Log.Debug(Tag, $"RegId: [{id}]");

                // If the application successfully connects with the push service,
                // it must request the unread notification messages
                // which are sent during the disconnected state
                // handlerNotification method is called if there are unread notifications
                PushClient.GetUnreadNotifications();
            }
            else if (e.State == PushConnectionStateEventArgs.PushState.Unregistered)
            {
                newState = State.Unregistered;

                Log.Debug(Tag, "Call PushServiceRegister()");

                // Send a registration request to the push service
                Task <ServerResponse> registerTask = PushClient.PushServerRegister();
                registerTask.GetAwaiter().OnCompleted(() =>
                {
                    // You will get result for register API
                    ServerResponse res = registerTask.Result;
                    Log.Debug(Tag, $"ServerResult: [{res.ServerResult}], ServerMessage: [{res.ServerMessage}]");
                });
            }

            RegistrationStateChanged?.Invoke(this, new RegistrationStateChangedEventArgs
            {
                state = newState
            });
        }
Exemplo n.º 2
0
        public int PushConnect()
        {
            // Use your push app id received from Tizen push team
            string AppId = "Your Push App ID";

            try
            {
                // Connect to local push service
                PushClient.PushServiceConnect(AppId);

                // When the connection state is changed, this handler will be called (ex, registered -> unregistered state)
                EventHandler <PushConnectionStateEventArgs> handler = (s, e) =>
                {
                    Console.WriteLine("Push State: [" + e.State + "], Error String: [" + e.Error + "]");

                    if (e.State == PushConnectionStateEventArgs.PushState.Registered)
                    {
                        // 0 is for registered state
                        pEvent.OnStateChanged(0);

                        // Get the registration id
                        string id = PushClient.GetRegistrationId();
                        Console.WriteLine("RegId: [" + id + "]");

                        // send registration id to your application server if necessary

                        // If the connection with the push service succeeds,
                        // the application must request the unread notification messages
                        // which are sent during the disconnected state
                        // handlerNotification method is called if there are unread notifications
                        PushClient.GetUnreadNotifications();
                    }
                    else if (e.State == PushConnectionStateEventArgs.PushState.Unregistered)
                    {
                        // 1 is for registered state
                        pEvent.OnStateChanged(1);

                        Console.WriteLine("Call PushServiceRegister()");

                        // Send a registration request to the push service
                        Task <ServerResponse> tr = PushClient.PushServerRegister();
                        tr.GetAwaiter().OnCompleted(() =>
                        {
                            // You will get result for register API
                            ServerResponse res = tr.Result;
                            Console.WriteLine("ServerResult: [" + res.ServerResult + "], ServerMessage: [" + res.ServerMessage + "]");
                        });
                    }
                };

                // When push notification is received, this handler will be called
                EventHandler <PushMessageEventArgs> handlerNotification = (object sender, PushMessageEventArgs e) =>
                {
                    Console.WriteLine("========================== Notification Received ==========================");
                    // App data loaded on the notification
                    Console.WriteLine("AppData: [" + e.AppData + "]");
                    // Notification message
                    Console.WriteLine("Message: [" + e.Message + "]");
                    // Time when the noti is generated/
                    Console.WriteLine("ReceivedAt: [" + e.ReceivedAt + "]");
                    // Optional sender information
                    Console.WriteLine("Sender: [" + e.Sender + "]");
                    // Optional session information
                    Console.WriteLine("SessionInfo: [" + e.SessionInfo + "]");
                    // Optional request ID
                    Console.WriteLine("RequestId: [" + e.RequestId + "]");
                    // Optional type
                    Console.WriteLine("Type: [" + e.Type + "]");
                    Console.WriteLine("===========================================================================");

                    // send notification received event to common interface
                    pEvent.OnNotificationReceived(e.AppData, e.Message, e.ReceivedAt, e.Sender, e.SessionInfo, e.RequestId, e.Type);
                };
                PushClient.StateChanged         += handler;
                PushClient.NotificationReceived += handlerNotification;
            }
            catch (Exception e)
            {
                // Exception handling
                Console.WriteLine("Caught Exception: " + e.ToString());
            }

            return(0);
        }