2017-07-31 98 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

相关问题