Subversion Repositories SmartDukaan

Rev

Rev 17210 | Rev 17345 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17210 Rev 17232
Line 395... Line 395...
395
				$this->NotificationCampaign->create();
395
				$this->NotificationCampaign->create();
396
				$data = array('name'=>$message['name'],'title'=>$message['title'],'type'=>$message['type'],'message'=>$message['message'],'url'=>$message['url'],'expiresat'=>$message['expiresat']);
396
				$data = array('name'=>$message['name'],'title'=>$message['title'],'type'=>$message['type'],'message'=>$message['message'],'url'=>$message['url'],'expiresat'=>$message['expiresat']);
397
				if($this->NotificationCampaign->save($data)){
397
				if($this->NotificationCampaign->save($data)){
398
					$message['cid'] = $this->NotificationCampaign->getLastInsertId();
398
					$message['cid'] = $this->NotificationCampaign->getLastInsertId();
399
				} else{
399
				} else{
400
					debug($this->NotificationCampaign->validationErrors);
-
 
401
					$message['cid'] = $message['name'];
400
					$message['cid'] = $message['name'];
402
				}
401
				}
-
 
402
				/*				
403
				if($message['type'] == 'url' && !empty($message['url'])) {
403
				if($message['type'] == 'url' && !empty($message['url'])) {
404
					$url = $message['url'];
404
					$url = $message['url'];
405
					$this->loadModel('Store');
405
					$this->loadModel('Store');
406
					$store = $this->Store->getByUrl($url);					
406
					$store = $this->Store->getByUrl($url);					
-
 
407
					$message['url'] = $this->generateAffiliateUrl($url,$value,$store);
407
				}				
408
				}				
408
				$this->loadModel('GcmUser');
409
				$this->loadModel('GcmUser');
409
				foreach ($this->request->data['userIds'] as $key => $value) {					
410
				foreach ($this->request->data['userIds'] as $key => $value) {					
410
					$options = array('conditions'=>array('user_id'=>$value),'fields'=>array('gcm_regid'),'order'=>array('id'=>'desc'));
411
					$options = array('conditions'=>array('user_id'=>$value),'fields'=>array('gcm_regid'),'order'=>array('id'=>'desc'));
411
					$gcmUser = $this->GcmUser->find('first',$options);
412
					$gcmUser = $this->GcmUser->find('first',$options);
Line 414... Line 415...
414
					if($message['type'] == 'url' && !empty($message['url'])) {
415
					if($message['type'] == 'url' && !empty($message['url'])) {
415
						$message['url'] = $this->generateAffiliateUrl($url,$value,$store);
416
						$message['url'] = $this->generateAffiliateUrl($url,$value,$store);
416
					}					
417
					}					
417
					$this->send_push_notification($regIds,$message,$value);
418
					$this->send_push_notification($regIds,$message,$value);
418
				}
419
				}
-
 
420
				*/
-
 
421
				$sentUsers = array();
-
 
422
				$payload = array();
-
 
423
				foreach ($this->request->data['userIds'] as $key => $value) {	
-
 
424
					if(!in_array($value, $sentUsers)) {
-
 
425
						$sentUsers[] = $value;								
-
 
426
						$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$value['User']['id'],'type'=>'pending','status'=>0);						
-
 
427
						$payload[] = $data;
-
 
428
					}
-
 
429
				}
-
 
430
				$url = Configure::read('nodeurl')."/addPushNotification";
-
 
431
				$this->make_request($url,json_encode($payload,JSON_NUMERIC_CHECK));
419
			}
432
			}
420
		}
433
		}
421
	}
434
	}
422
 
-
 
423
	/*private function send_push_notification($registatoin_ids, $message, $user_id) {
-
 
424
		$msg = array(
-
 
425
		    'message'       => $message['message'],
-
 
426
		    'cid'       	=> $message['cid'],
-
 
427
		    'title'         => $message['title'],
-
 
428
		    'type'      	=> $message['type'],
-
 
429
		    'url'		    => $message['url'],
-
 
430
		    // 'expiresat'	    => strtotime($message['expiresat']),
-
 
431
		    'vibrate'       => 1,
-
 
432
		    'sound'         => 1,
-
 
433
		    'largeIcon'     => 'large_icon',
-
 
434
		    'smallIcon'     => 'small_icon'
-
 
435
		);
-
 
436
		// Set POST variables
-
 
437
		$url = 'https://android.googleapis.com/gcm/send';
-
 
438
 
-
 
439
		$fields = array(
-
 
440
		    'registration_ids' => $registatoin_ids,
-
 
441
		    'data' => $msg,
-
 
442
		);
-
 
443
		$headers = array(
-
 
444
		    'Authorization: key=' . Configure::read('googleapikey'),
-
 
445
		    'Content-Type: application/json'
-
 
446
		);
-
 
447
		//print_r($headers);
-
 
448
		// Open connection
-
 
449
		$ch = curl_init();
-
 
450
 
-
 
451
		// Set the url, number of POST vars, POST data
-
 
452
		curl_setopt($ch, CURLOPT_URL, $url);
-
 
453
		curl_setopt($ch, CURLOPT_POST, true);
-
 
454
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
 
455
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
 
456
 
-
 
457
		// Disabling SSL Certificate support temporarly
-
 
458
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-
 
459
 
-
 
460
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
-
 
461
 
435
	
462
		// Execute post
-
 
463
		$result = curl_exec($ch);
-
 
464
		if ($result === FALSE) {
-
 
465
		    die('Curl failed: ' . curl_error($ch));
-
 
466
		}
-
 
467
		$res = json_decode($result,1);
-
 
468
		$this->loadModel('Pushnotification');
-
 
469
		$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$user_id,'status'=>$res['success']);
-
 
470
		$this->Pushnotification->create();
-
 
471
		$this->Pushnotification->save($data);
-
 
472
		// Close connection
-
 
473
		curl_close($ch);
-
 
474
		$this->log("For $user_id ".$registatoin_ids[0]." ".print_r($result,1),'pushnotifications');
-
 
475
	}*/
-
 
476
	private function send_push_notification($registatoin_ids, $message, $user_id) {
436
	private function send_push_notification($registatoin_ids, $message, $user_id) {
477
                $msg = array(
437
                $msg = array(
478
                    'message'       => $message['message'],
438
                    'message'       => $message['message'],
479
                    'cid'               => $message['cid'],
439
                    'cid'               => $message['cid'],
480
                    'title'         => $message['title'],
440
                    'title'         => $message['title'],