void updateView() { //Get the stored latest registration id var registrationId = PushClient.GetRegistrationId(this); //If it's empty, we need to register if (string.IsNullOrEmpty(registrationId)) { registered = false; this.textRegistrationStatus.Text = "Registered: No"; this.textRegistrationId.Text = "Id: N/A"; this.buttonRegister.Text = "Register..."; Log.Info("C2DM-Sharp", "Not registered..."); } else { registered = true; this.textRegistrationStatus.Text = "Registered: Yes"; this.textRegistrationId.Text = "Id: " + registrationId; this.buttonRegister.Text = "Unregister..."; Log.Info("C2DM-Sharp", "Already Registered: " + registrationId); } var prefs = GetSharedPreferences("c2dm.client.sample", FileCreationMode.Private); this.textLastMsg.Text = "Last Msg: " + prefs.GetString("last_msg", "N/A"); //Enable the button as it was normally disabled this.buttonRegister.Enabled = true; }
public void RegisterDeviceForPushNotifications(bool force = false) { //Check to ensure everything's setup right PushClient.CheckDevice(_context); PushClient.CheckManifest(_context); //Get the stored latest registration id var registrationId = PushClient.GetRegistrationId(_context); var registered = !string.IsNullOrEmpty(registrationId); const string tag = "PushSharp-GCM"; if (!registered || force) { //Call to register var registerIdDebug = _appSettings.Data.GCM.SenderId; PushClient.Register(_context, registerIdDebug); } }
/// <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 }); }
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); }
/// <summary> /// Used to get registration id /// </summary> /// <returns>registration id</returns> public string PushGetRegistrationId() { return(PushClient.GetRegistrationId());; }