2017-07-31 95 views
11

我正在構建一個使用Ionic Framework的應用程序,該應用程序實現了一個類似於古老的Facebook Messenger的聊天功能,因爲我想通知用戶的聊天消息,但如果他們在其他地方查看它,我想從主屏幕中刪除通知。創建本地通知以響應科爾多瓦/離子中的推送通知(來自firebase)

我使用firebase作爲推送通知的後端(雖然可以改變我的假設)。

我知道你不能過期的遠程通知,但我被告知你可以過期+刪除本地通知,所以我的問題是 - 我可以可靠地接收遠程通知,創建一個本地通知,並顯示該信息,然後爲了響應範圍爲「過期」或「刪除」的通知,刪除本地通知,以便我的用戶看不到重複的信息?

大多數插件傾向於檢測應用程序的狀態,並使用默認推送的信息向主屏幕添加遠程通知,有沒有辦法避免這種情況?

謝謝你們。

編輯: - 本地通知:http://ionicframework.com/docs/native/local-notifications/ - 火力地堡雲消息:https://github.com/fechanique/cordova-plugin-fcm

+1

如何將遠程通知轉換爲本地通知? – Pritish

回答

4

在科爾多瓦/離子通知唯一棘手位是JS部接收到通知和觸發的Android代碼。

我用https://github.com/phonegap/phonegap-plugin-push庫和它非常簡單。

當在JS(Cordova/Ionic)中收到通知時有一個回調,使用它在Android中本地呈現通知。

P.S:巴塞爾的答案告訴你如何清除你的通知,所以我決定離開那一點。

5

據我所知,沒有插件可以完成您所需要的任何功能。然而..

我可以可靠地接收到一個遠程通知,創建一個本地的,然後顯示,然後迴應一個範圍'expire'或'remove'的通知,刪除一個本地通知我的用戶沒有看到重複的信息?

大多數插件傾向於檢測應用程序的狀態,並使用默認推送的信息向主屏幕添加遠程通知,有沒有辦法避免這種情況?

是,通過silent notifications和自己建立本地通知。

對於一個項目我工作中,我修改了插件cordova-plugin-fcm添加支持(本地需求)的通知解僱/顯示,發送多個通知到科爾多瓦的應用程序,而且尚未包括一些永久居民。此外,我自己編寫通知,以全面控制顯示的內容。你可以看看代碼來獲得一些想法。

總之它的工作原理是這樣的:

首先,我送一個「沉默」推到應用程序,這是不是由Android顯示:

{ 
    "content_available": true, // IMPORTANT: For Apple -> content-available: 1, for firebase -> content_available: true 
    "priority": "high", 
    "to": "/topics/all", // or to a fcm token 
    "data"{ 
     "title": "My title", // this implies that you display the notification by yourself 
     "body": "My body", // this implies that you display the notification by yourself 
     "type": "NEW_USER_MESSAGE", // only relevant to this project 
     "userId": "1", // only relevant to this project 
     "timestamp", "150000000" 
    } 
} 

注:如果有效載荷有"notification": {}項目,Android會在系統托盤上顯示它(如果應用程序在後臺)。其次,當推送到達應用程序(在onMessageReceived())時,我建立本地通知,爲它分配一個TAG和一個ID。這是您可以稍後使用的方式。 例如,您可以使用標記「NEW_USER_MESSAGE」和ID 1(例如表示消息狀態的常量或用戶標識)創建本地通知。此外,Android will replace notifications with the same TAG and ID,所以這是另一種自動替換通知的方式(例如,如果您發送通用消息,如「新的更新可用」)。這樣做的

public static String TYPE_NEW_USER_MESSAGE = "NEW_USER_MESSAGE"; 
    public static String TYPE_USER_LEFT_ROOM = "USER_LEFT_ROOM"; 

    NotificationManager notificationManager = 
      (NotificationManager) _ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

    // based in the type of the message you've received, you can stylize the notification 
    if (type.equals(TYPE_USER_LEFT_ROOM)){ 
     notificationBuilder.setColor(Color.RED); 
     notificationBuilder.setLights(Color.RED, 1000, 500); 
    } 
    else if (type.equals(TYPE_NEW_USER_MESSAGE)){ 
     notificationBuilder.setColor(Color.BLUE); 
     notificationBuilder.setLights(Color.BLUE, 1000, 1000); 
    } 

    Notification n = notificationBuilder.build(); 
    notificationManager.notify(type, userId, n); 

一個優點,就是你必須要顯示的通知的完全控制,這樣你就可以設置樣式像你想要的。

如果要丟棄過期的消息,您可以check out the elapsed time between the sent timestamp and the current timestamp

java.util.Date now = new java.util.Date(); 
java.util.Date sent_timestamp = new java.util.Date(Long.valueOf(timestamp.toString())); 
      final Long elapsed_time = ((now.getTime() - sent_timestamp.getTime())/1000); 
Log.d(TAG, "New message. sent " + elapsed_time + "s ago"); 

第三,當通知用戶點擊Android將啓動您的應用程序,該插件會推送消息的有效載荷發送到科爾多瓦視圖(onNotificationReceived())。

一旦程序被打開,你收到推送消息時,可以關閉它加入了新的動作,插件:

onNotificationReceived(data){ 
    if (data.wasTapped === true){ 
     if (data.type === 'NEW_USER_MESSAGE'){ 
      FCMPlugin.dismissNotification(NEW_USER_MESSAGE, 1); 
     } 
    } 
} 

的Android操作:

else if (action.equals(ACTION_DISMISS_NOTIFICATION)) { 
    cordova.getThreadPool().execute(new Runnable() { 
     public void run() { 
      try{ 
       Log.d(TAG, "FCMPlugin dismissNotificaton: " + args.getString(0)); //tag 
       NotificationManager nManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
         nManager.cancel(args.getString(0)/*NEW_USER_MESSAGE*/, args.getInt(1) /*1*/); 
       Log.d(TAG, "FCMPlugin dismissNotificaton() to remove: " + id); //tag 
       callbackContext.success(); 
      }catch(Exception e){ 
       callbackContext.error(e.getMessage()); 
      } 
     } 
}); 

https://github.com/TrustedCircles/cordova-plugin-fcm/blob/master/src/android/FCMPlugin.java#L286

並暴露給科爾多瓦應用程序的方法:

// dismisses a notification by tag+id 
FCMPlugin.prototype.dismissNotification = function(tag, userId, success, error){ 
    exec(success, error, "FCMPlugin", 'dismissNotification', [tag, userId]); 
} 

https://github.com/TrustedCircles/cordova-plugin-fcm/blob/master/www/FCMPlugin.js#L65

相關問題