[Push] connectService
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Thu, 12 Feb 2015 12:41:09 +0000 (13:41 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 20 Feb 2015 12:36:31 +0000 (21:36 +0900)
Change-Id: I52989b6826c50f54e19b280ac35cb451ac0d7dc9
Signed-off-by: Przemyslaw Ciezkowski <p.ciezkowski@samsung.com>
packaging/webapi-plugins.spec
src/push/push.gyp
src/push/push_api.js
src/push/push_instance.cc
src/push/push_instance.h
src/push/push_manager.cc
src/push/push_manager.h

index 392686c..f797c65 100755 (executable)
@@ -209,6 +209,7 @@ BuildRequires: pkgconfig(capi-system-power)
 %if 0%{?tizen_feature_power_support}
 BuildRequires: pkgconfig(push)
 BuildRequires: pkgconfig(capi-appfw-application)
+BuildRequires: pkgconfig(libpcrecpp)
 %endif
 
 %if 0%{?tizen_feature_tv_display_support}
index 7e57a01..1192230 100644 (file)
@@ -24,6 +24,7 @@
             'packages': [
               'push',
               'capi-appfw-application',
+              'libpcrecpp',
             ]
           },
         }],
index 3c632b0..caaff7a 100644 (file)
@@ -20,7 +20,7 @@ function PushMessage(dict) {
   for (var key in dict) {
     if (dict.hasOwnProperty(key)) {
       Object.defineProperty(this, key, {
-        value: dict[key],
+        value: key === 'date' ? new Date(dict[key] * 1000) : dict[key],
         enumerable: true
       });
     }
index e01de2d..d6edec3 100644 (file)
@@ -11,7 +11,8 @@
 namespace extension {
 namespace push {
 
-PushInstance::PushInstance() {
+PushInstance::PushInstance():
+        m_ignoreNotificationEvents(true) {
     LoggerD("Enter");
     using std::placeholders::_1;
     using std::placeholders::_2;
@@ -19,7 +20,7 @@ PushInstance::PushInstance() {
             std::bind(&PushInstance::registerService, this, _1, _2));
     RegisterHandler("Push_unregisterService",
             std::bind(&PushInstance::unregisterService, this, _1, _2));
-    RegisterHandler("Push_connectService",
+    RegisterSyncHandler("Push_connectService",
             std::bind(&PushInstance::connectService, this, _1, _2));
     RegisterSyncHandler("Push_disconnectService",
             std::bind(&PushInstance::disconnectService, this, _1, _2));
@@ -78,6 +79,7 @@ void PushInstance::unregisterService(const picojson::value& args,
 void PushInstance::connectService(const picojson::value& args,
         picojson::object& out) {
     LoggerD("Enter");
+    m_ignoreNotificationEvents = false;
     picojson::value result;
     ReportSuccess(result, out);
 }
@@ -85,6 +87,7 @@ void PushInstance::connectService(const picojson::value& args,
 void PushInstance::disconnectService(const picojson::value& args,
         picojson::object& out) {
     LoggerD("Enter");
+    m_ignoreNotificationEvents = true;
     picojson::value result;
     ReportSuccess(result, out);
 }
@@ -117,6 +120,23 @@ void PushInstance::onPushRegister(double callbackId,
     PostMessage(res.serialize().c_str());
 }
 
+void PushInstance::onPushNotify(const std::string& appData,
+        const std::string& alertMessage, double date) {
+    LoggerD("Enter");
+    if (m_ignoreNotificationEvents) {
+        LoggerD("Listener not set, ignoring event");
+    }
+    picojson::value::object dict;
+    dict["listenerId"] = picojson::value("Push_Notification_Listener");
+    picojson::value::object pushMessage;
+    pushMessage["appData"] = picojson::value(appData);
+    pushMessage["alertMessage"] = picojson::value(alertMessage);
+    pushMessage["date"] = picojson::value(date);
+    dict["pushMessage"] = picojson::value(pushMessage);
+    picojson::value resultListener(dict);
+    PostMessage(resultListener.serialize().c_str());
+}
+
 PushInstance::~PushInstance() {
     LoggerD("Enter");
 }
index d110630..70e2311 100644 (file)
@@ -18,6 +18,9 @@ class PushInstance: public common::ParsedInstance, public EventListener {
     virtual ~PushInstance();
     virtual void onPushRegister(double callbackId,
             common::PlatformResult result, const std::string& id);
+    virtual void onPushNotify(const std::string& appData,
+            const std::string& alertMessage, double date);
+
  private:
      void registerService(const picojson::value& args, picojson::object& out);
      void unregisterService(const picojson::value& args, picojson::object& out);
@@ -26,6 +29,8 @@ class PushInstance: public common::ParsedInstance, public EventListener {
      void getRegistrationId(const picojson::value& args, picojson::object& out);
      void getUnreadNotifications(const picojson::value& args,
             picojson::object& out);
+
+     bool m_ignoreNotificationEvents;
 };
 
 }  // namespace push
index 3b3d733..41d145d 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "push/push_manager.h"
 #include <unistd.h>
+#include <pcrecpp.h>
 #include <app_control.h>
 #include <app_manager.h>
 #include "common/logger.h"
@@ -175,6 +176,48 @@ void PushManager::onPushState(push_state_e state, const char* err,
 
 void PushManager::onPushNotify(push_notification_h noti, void* user_data) {
     LoggerD("Enter");
+    if (!getInstance().m_listener) {
+        LoggerW("Listener not set, ignoring");
+        return;
+    }
+
+    char* temp = NULL;
+    int ret = push_get_notification_data(noti, &temp);
+    if (ret != PUSH_ERROR_NONE) {
+        LoggerE("Failed to get appData");
+        return;
+    }
+    std::string appData = temp;
+    free(temp);
+
+    temp = NULL;
+    ret = push_get_notification_message(noti, &temp);
+    if (ret != PUSH_ERROR_NONE) {
+        LoggerE("Failed to get message");
+        return;
+    }
+
+    // parse query string and find value for alertMessage
+    pcrecpp::StringPiece input(temp);
+    pcrecpp::RE re("([^=]+)=([^&]*)&?");
+    string key;
+    string value;
+    std::string alertMessage;
+    while (re.Consume(&input, &key, &value)) {
+        if (key == "alertMessage") {
+            alertMessage = value;
+            break;
+        }
+    }
+    free(temp);
+
+    int64_t date = -1;
+    ret = push_get_notification_time(noti, &date);
+    if (ret != PUSH_ERROR_NONE) {
+        LoggerE("Failed to get date");
+        return;
+    }
+    getInstance().m_listener->onPushNotify(appData, alertMessage, date);
 }
 
 void PushManager::onPushRegister(push_result_e result, const char* msg,
index 58819fc..f45876a 100644 (file)
@@ -18,6 +18,8 @@ class EventListener {
  public:
     virtual void onPushRegister(double callbackId,
             common::PlatformResult result, const std::string& id) = 0;
+    virtual void onPushNotify(const std::string& appData,
+            const std::string& alertMessage, double date) = 0;
     virtual ~EventListener() {}
 };