Answers for "push notification tutorial"

1

register for remote notifications swift

// Import the UserNotifications framework and add the UNUserNotificationCenterDelegate in AppDelegate.swift
import UserNotifications 

// Request user permission

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()
        return true
}

// Getting device token

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print(deviceTokenString)
}

// In case of error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

        print("i am not available in simulator \(error)")
}

// In case if you need to know the permissions granted

UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in

	switch settings.soundSetting{
      	case .enabled:
	      	print("enabled sound setting")
		case .disabled:
      		print("setting has been disabled")
      	case .notSupported:
      		print("something vital went wrong here")
    }
}
Posted by: Guest on January-31-2020
3

push notification

public function push($device_data, $message, $push_params) {
  $ch = curl_init();

  // Set POST variables
  $this->set_credential(true);
  $url = $this->server_feedback_url;

  $headers = array(
    'Connection: keep-alive',
    'Authorization: key=' . $this->server_key,
    'Content-Type: application/json'
  );

  $fields  = array();

  $failed_case = array();
  $succeed_case = array();

  try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // device_data
    // Array
    // (
    // 	[0] => arrayfSiW2K4tzpY:APA91bGxwkrBAI2FR3Dt5iAMwfhxLeCdSz62HCzF_4xRT4dcWw61bQxUjV7T0JK4hSKYTP2wG--A_pWGAfXlxH9vO68rR_h0crChNnGR0vnDUkgpe2KzGNsZgICEuYX0xCs5KilX3RhJ
    // 	[1] => fSiW2K4tzpY:APA91bGxwkrBAI2FR3Dt5iAMwfhxLeCdSz62HCzF_4xRT4dcWw61bQxUjV7T0JK4hSKYTP2wG--A_pWGAfXlxH9vO68rR_h0crChNnGR0vnDUkgpe2KzGNsZgICEuYX0xCs5KilX3RhJ
    // )

    // $fields = array(
    // 	// 'to' => $device_data[0]['device_token'],
    // 	'registration_ids' => $device_data,	// $device_data[0]['device_token'],
    // 	'notification' 	=> array(
    // 		'body'		=> $message['notification']['body'],
    // 		'title'		=> $message['notification']['title'],
    // 	),
    // 	'priority' => 'high',
    // 	'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',

    //   );

    $fields = array(
      'registration_ids' => $device_data,		// array
      'notification' 	=> array(
        'body'		=> $message['notification']['body'],
        'title'		=> $message['notification']['title'],
      ),
      'priority' => 'high',
      'data' => isset($push_params['custom_data']) ? $push_params['custom_data'] : '',

    );

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);

    // {"multicast_id":9114866147884203417,"success":1,"failure":3,"canonical_ids":0,
    //	"results":[{"message_id":"0:1576208394681875%cb43adbbcb43adbb"},{"error":"InvalidRegistration"},
    //				{"error":"NotRegistered"},{"error":"NotRegistered"}]}

    $temp = json_decode($result, true);
    if ($temp['failure'] == 0) {		// dont have fail case
      $succeed_case = $device_data;
      $failed_case = array();

    } else {		// exist fail case
      $index = 0;
      foreach ($temp['results'] as $value) {

        if (isset($value['error'])) {
          $failed_case[] = $device_data[$index];

        } else {
          $succeed_case[] = $device_data[$index];
        }

        $index = $index + 1;
      }
    }

    $pushed = array(
      'status' => true,
      'params' => array(
        'result'		=> $result,
        'succeed' 		=> $succeed_case,
        'failed' 		=> $failed_case,
      ),
    );
  } catch (Exception $e) {
    $pushed = array(
      'status' => false,
      'params' => array(),
      'error_messages' => $e->getMessage(),
    );

  } finally {
    curl_close($ch);	 // Close the connection to the server
  }

  return $pushed;
}

public function set_credential($sandbox = true) {
  if ($sandbox === true) {
    $this->server_key 				= Environment::read('push.server_key');
    $this->sender_id	 			= Environment::read('push.sender_id');
    $this->server_feedback_url 		= Environment::read('push.server_feedback_url');
  }
}
Posted by: Guest on October-02-2020

Code answers related to "push notification tutorial"

Code answers related to "Swift"

Browse Popular Code Answers by Language