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

据我所知,没有插件可以满足你的需要。 然而..

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

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

是的,通过使用静默通知并自行构建本地通知。

对于我正在进行的项目,我修改了插件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" } }

其次,当推送到应用程序时(在onMessageReceived()中 ),我构建本地通知,为其分配TAG和ID。 这是您以后可以用来解除它的方式。 例如,您可以使用TAG“NEW_USER_MESSAGE”和ID 1(表示消息状态的常量或例如用户ID)创建本地通知。 此外, Android将使用相同的TAG和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);

以这种方式执行此操作的一个优点是,您可以完全控制要显示的通知,因此您可以根据需要对其进行样式化。

如果要丢弃过期的消息,可以查看发送的时间戳和当前时间戳之间经过的时间 :

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()); } } });

并且该方法暴露给cordova app:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值