创建本地通知以响应cordova / ionic中的推送通知(来自firebase)

时间:2017-07-31 01:13:01

标签: android ios cordova ionic-framework

我正在使用Ionic Framework构建一个应用程序,它实现类似于好老的facebook messenger的聊天功能,因为我想通知用户聊天消息,但如果他们在其他地方查看,我想删除通知从他们的主屏幕。

我正在使用firebase作为推送通知的后端(虽然我想这可以改变)。

我知道您不能过期远程通知,但我被告知您可以过期+删除本地通知,所以我的问题是 - 我能否可靠地接收远程通知,创建本地通知,并显示该内容,然后在响应“过期”或“删除”范围的通知时,删除本地通知,以便我的用户看不到重复的信息?

大多数插件都会检测应用程序的状态,并使用您默认推送的信息向主屏幕添加远程通知,有没有办法避免这种情况?

谢谢你们。

编辑: - 本地通知:http://ionicframework.com/docs/native/local-notifications/ - Firebase云消息传递:https://github.com/fechanique/cordova-plugin-fcm

2 个答案:

答案 0 :(得分:5)

据我所知,没有任何插件可以满足您的所有需求。然而..

  

我能否可靠地接收远程通知,创建本地远程通知并显示该通知,然后响应范围为“到期”的通知。或者'删除',删除本地通知,以便我的用户不会看到重复的信息?

     

大多数插件都会检测到应用的状态,并使用您默认推送的信息向主屏幕添加远程通知,有没有办法避免这种情况?

是的,使用silent notifications并自行构建本地通知。

对于我正在工作的项目,我修改了插件cordova-plugin-fcm以添加对(本地点播)通知的支持解除/显示,向cordova app发送多个通知,以及一些PR尚未包括在内。此外,我自己构建通知,以完全控制显示的内容。您可以查看代码以获得一些想法。

简而言之,它的工作原理如下:

首先,我发送一个"沉默"推送到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将在系统托盘上显示它(如果应用程序处于后台)。 https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

其次,当推送到应用程序(在onMessageReceived()中)时,我构建本地通知,为其分配TAG和ID。这是您以后可以用来解散它的方法。 例如,您可以使用TAG" NEW_USER_MESSAGE"创建本地通知。和ID 1(表示消息状态的常量,例如用户ID)。另外,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会启动您的应用,插件会将推送消息的有效负载发送到cordova视图(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

该方法暴露给cordova应用程序:

// 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

答案 1 :(得分:3)

在cordova / ionic中通知的唯一棘手的部分是JS部分接收通知并触发Android代码。

我使用了https://github.com/phonegap/phonegap-plugin-push库,非常直接。

在JS(Cordova / Ionic)收到通知时有一个回调,用它在Android中本地呈现通知。

P.S:巴塞尔的回答告诉你如何清除你的通知,所以我决定把这一点留下来。