Firefox WebExtension notifications API: How to call a function when the notification is clicked - firefox

I'm trying to develop a Firefox add on using WebExtensions. What I'm trying to do is open a new Firefox tab or window when the user clicks the notification. But it doesn't work.
When I click the notification, nothing happens.
I'm creating notifications like:
var q = chrome.notifications.create({
"type": "basic",
"iconUrl": chrome.extension.getURL("128-128q.png"),
"title": 'title',
"message": 'content'
});
chrome.notifications.onClicked.addListener(function(notificationId) {
window.open('http://www.google.com');
});
browser.notifications.onClicked.addListener(function(notificationId) {
window.open('http://www.google.com');
});
q.onClicked.addListener(function(notificationId) {
window.open('http://www.google.com');
});
var audio = new Audio('message.mp3');
audio.play();
How can I make this work?

It appears that the problem is that you are attempting to open a new URL in a way that does not work.
The following should work, using chrome.tabs.create():
var q = chrome.notifications.create("MyExtensionNotificationId", {
"type": "basic",
"iconUrl": chrome.extension.getURL("128-128q.png"),
"title": 'title',
"message": 'content'
});
chrome.notifications.onClicked.addListener(function(notificationId) {
chrome.tabs.create({url:"http://www.google.com"});
});
However, you need to be testing this in Firefox 47.0+ as support for chrome.notifications.onClicked() was only added recently. My statement of Firefox 47.0+ is based on the compatibility table. However, the compatibility table has at least one definite error. Thus, a higher version of Firefox may be required. The code I tested worked in Firefox Nightly, version 50.0a1, but did not work properly in Firefox Developer Edition, version 48.0a2. In general, given that the WebExtensions API is in active development, you should be testing against Firefox Nightly if you have questions/issues which are not functioning as you expect. You can also check the source code for the API to see what really is implemented and when it was added.
Note: this works with either chrome.notifications.onClicked or browser.notifications.onClicked. However, don't use both to add two separate anonymous functions which do the same thing as doing so will result in whatever you are doing happening twice.
I did not actually test it with your code, but I did test it with a modified version of the notify-link-clicks-i18n WebExtensions example. I modified the background-script.js file from that example to be:
/*
Log that we received the message.
Then display a notification. The notification contains the URL,
which we read from the message.
*/
function notify(message) {
console.log("notify-link-clicks-i18n: background script received message");
var title = chrome.i18n.getMessage("notificationTitle");
var content = chrome.i18n.getMessage("notificationContent", message.url);
let id = "notify-link-clicks-i18n::" + title + "::" + message.url;
chrome.notifications.create(id,{
"type": "basic",
"iconUrl": chrome.extension.getURL("icons/link-48.png"),
"title": title,
"message": content
});
}
/*
Assign `notify()` as a listener to messages from the content script.
*/
chrome.runtime.onMessage.addListener(notify);
//Add the listener for clicks on a notification:
chrome.notifications.onClicked.addListener(function(notificationId) {
console.log("Caught notification onClicked with ID:" + notificationId);
//Open a new tab with the desired URL:
browser.tabs.create({url:"http://www.google.com"});
});
My preference for something like this where a unique ID is possible is to provide a unique ID, that both identifies that the notification was displayed by my add-on and what the notification was. This allows the listener function to choose to only act on notifications which were displayed by my add-on and/or only a subset of those I display, or have different actions based on why I displayed the notification.
The reason you had trouble here in figuring out what was not working is probably because you did not reduce the issue to the minimum necessary to demonstrate the issue. In other words, it would have been a good idea to just try opening a new URL separately from the notifications onClicked listener and just try a console.log() within the listener.

Related

Xamarin Forms using FirebasePushNotificationPlugin plugin with Autofac and Shell unable to display UI elements nor redirects

I'm experiencing an issue that I've tried different ways to resolve and have been unsuccessful with. Whenever receiving a message via CrossFirebasePushNotification.Current.OnNotificationReceived I get the payload and can read it, but I can't do anything "visual" in my app. In other words, I get the notification but when I want to either redirect to another page or even display a DisplayAlert message, nothing happens. MessagingCenter can be received, but any instructions within the procedure that have to do with redirecting or DisplayAlert (anything changing the UI it appears) will not be executed.
Looking for help in how to visually show in the app the user has received a message.
This is in MainShell.xaml.cs (I've also placed it in App.xaml.cs too):
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received"); // this works
foreach (var data in p.Data)
{
System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}"); // this works
}
//TODO: Code to determine if user is signed in
if (true)
{
System.Diagnostics.Debug.WriteLine("Debug: Goto Registration page from MainShell"); // this works
//Task.Run(async () => await Shell.Current.GoToAsync("registration")); // this does not work (just ignores it and doesn't fire constructor)
//Task.Run(async () => await Current.DisplayAlert("Request Received", "A message is waiting...", "Ok")); // this does not work
MessagingCenter.Send(this, "RequestReceived"); // this works, the receiver will display debug info, but will not redirect nor display any UI messages or interact with the UI.
}
};
Thank you for any help or guidance,
Robert

SAPCAI quick replies displayed as attachments

I used SAPCAI (SAP Conversational AI is a French development platform like Azure Bot Service) to build my chatbot, but I'm using the Bot-Framework Webchat on my web app. Therefore, I don't have any C# or JS code. The problem is SAPCAI quick replies are displayed as "attachments". How can I fix that?
Expected
Got
What you want is the Quick replies type.
Quick replies: Same purpose as buttons, but disappear once clicked. Great if you don’t want the user to have to scroll up the conversation and click on a button again.
There is information about how to create click replies on this page. It seems to be in the format of:
{
"type": "quickReplies",
"content": {
"title": "TITLE",
"buttons": [
{
"title": "BUTTON_TITLE",
"value": "BUTTON_VALUE"
}
]
}
}
The important part would be "type": "quickReplies". Since you haven't provided any code I'm not sure if you know how to get to the stage where you enter/edit this JSON. From the documentation on the first page that I linked it would seem that you get to this via:
On the Actions tab of a skill (or on the Requirements tab), you can choose among other things to send messages.
Under the send message button you will be displayed a list of message types to send, quick replies is one of these types. See my screenshots here.
I hope this helps.
I'm not entirely sure how SAPCAI works, but if you are receiving a card in Web Chat, you can use a custom middleware store to convert the card's title to text and its buttons to suggested actions. Then you can add them to the activity in place of the attachment. Note, the store middleware below will convert all card to suggested actions, so you may want to add some additional logic if you do intend to use other cards in your dialog. Also, if the card is an AdaptiveCard, you will need to do some more modifications as well since those tend to more complex than rich cards.
Middleware
const store = createStore(
{},
({ dispatch}) => next => async action => {
if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
const { attachments, from: { role }} = action.payload.activity;
if (role === 'bot' && attachments) {
const text = attachments.map(({ content: { title }}) => title).join(' ');
const actions = attachments.map(({content: { buttons }}) => buttons).flat();
action.payload.activity.text = text;
action.payload.activity.attachments = [];
action.payload.activity.suggestedActions = { actions };
}
}
return next(action)
}
);
renderWebChat({
directLine,
store,
}, document.getElementById('webchat'));
Screenshot
Hope this helps!

How to get the current tab's history in a Web Extension in Firefox?

Is there an API that makes it possible to get the current tab's history in a Web Extension in Firefox? Just like when clicking and holding on the Back button, a dropdown will appear to show the current tab's history.
No. You cannot ask for the list for a certain tab by default.
You can, however, listen for the tab events onUpdated, onCreated etc. Using the tabId which stays the same, you can keep a list of URLs in a background script (background.js) which is always running if the addon is enabled.
You would do it like this:
let arr=[]; // At the top of background.js
browser.tabs.onCreated.addListener(handleCreated); // Somewhere in background.js
function handleCreated(tab) {
let tabId = tab.id;
if(arr[tabId]==null) arr[tabId] = [];
arr[tabId].push(url);
}
function getHistoryForCurrentTab(){
function currentTabs(tabs) {
// browser.tabs.query returns an array, lets assume the first one (it's safe to assume)
let tab = tabs[0];
// tab.url requires the `tabs` permission (manifest.json)
// We will now log the tab history to the console.
for(let url of arr[tab.id]){
console.log(url);
}
}
function onError(error) {
console.log(`This should not happen: ${error}`);
}
browser.tabs.query({currentWindow: true, active: true}).then(currentTabs, onError);
}
The above code is a proof of concept. Some improvements you will need to consider: implement onClosed which resets the tab history for that id (arr[tabId] = null), implement onUpdated (will be needed for sure, same logic as in handleCreated).
Links:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs

Firefox add-on triggers code in each open window

I have a Firefox add on that displays a pop-up when it sees a certain response header. This works fine when I have a single window. However, when I have multiple windows open, and one of the tabs in a window triggers the pop-up code in my add-on (due to the presence of said header) I get a pop-up in each of my open windows. For example, if I have 3 windows open, I get 3 different pop ups, one for each windows. Is this the default behavior, and is there an easy in-built way to fix this using their SDK.
Edit:
I have the following code:
Util.requestBlock(httpChannel) {
/*load response headers here*/
if (responseHeaders.includes("header_xyz"))
alert("show popup");
}
Util.monitor = function(w) {
this.obsService = Components.classes['#mozilla.org/observer-service;1'].getService(Components.interfaces.nsIObserverService);
this.obsService.addObserver(this, 'http-on-examine-response', false);
}
Util.monitor.prototype = {
'observe': function(subject, topic, data). {
if (topic == 'http-on-examine-response'). {
var channel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
var block_response = new Util.RequestBlock(channel);
}
},
};
The Util.monitor adds an observer. Whenever a response is received, the "Observe" function is called.
var windows = require("window-utils");
for (window in windows.browserWindowIterator)
doToWindow(window);

Kaltura Flavor Switching

I am currently using Kaltura HTML5 Player ver. 2.26. The documentation suggests that you can switch between video flavors via the "doSwitch" notification, like so:
kdp.sendNotification("doSwitch", { flavorIndex: 3 });
The video I am using has 6 different flavors according to kdp.evaluate("{mediaProxy.kalturaMediaFlavorArray}"), but running this with various different indices has no discernible effect. I would expect to see the kdp fire a switchingChangeStarted event, as happens when using the Source Selector plugin UI, but there's just silence.
Searching through the github repo for doSwitch, I don't actually see it implemented anywhere. Is this some method a lost relic? If not, how do I get the doSwitch notification to work?
KDP is the Kaltura flash player which has this notification for switching bitrates.
The notification is still being used internally when the chromeless flash player is loaded and the source selector button is clicked. But it doesn't look like a V2 player notification.
You could extend the player by adding a new plugin that will expose such a notification that will switch the source similar to how the source selector is doing it (sourceSelector.js::208):
_this.getPlayer().switchSrc( source )
Note that the list of sources could have sources that are not playable on desktop, you should not use those for switching.
For the sake of posterity, here is what I ended up doing, following Roman's suggestion. The plugin below is pretty much the bare minimum, but this does precisely what I want it to do.
In the embed, we need to declare the custom plugin:
kWidget.embed({
...
"flashvars": {
...
"sourceExposure": {
"plugin": true,
"iframeHTML5Js": "js/sourceExposure.js"
}
}
}
In js/sourceExposure.js, we need to declare a plugin that provides a response to a custom event (here, "customDoSwitch"):
(function(mw,$) {
mw.kalturaPluginWrapper(function() {
mw.PluginManager.add( 'sourceExposure', mw.KBaseComponent.extend({
setup: function() {
var _this = this;
this.bind('customDoSwitch', function(evt, flavorIndex) {
var sources = _this.getSources().slice(0)
if (flavorIndex >= sources.length) {
_this.log("Flavor Index too large.");
return;
}
_this.getPlayer.switchSrc(_this.getSources()[flavorIndex]);
})
},
getSources: function() {
return this.getPlayer().getSources()
}
}));
});
})(window.mw, window.jQuery)
When we want to switch to a different flavor, we can now use the custom event and pass the flavor index:
kdp.sendNotification("customDoSwitch", 2) //switches to flavor index 2

Resources