Reputation: 3773
I am able to send a message to a Chrome Web App using GCM. I am using a http post to: https://android.googleapis.com/gcm/send sending registrationid, applicationid, and senderid values. The message shows as a Chrome Notification on my screen. My question is - is there a command or a GCM event/function I can use to start the Chrome Web App? Google Chrome is set to run in the background.
This is my GCM Listener for message event:
chrome.gcm.onMessage.addListener(messageReceived);
It calls the messageReceived
function and in this function I get message, pop notification on the screen, and open/create the window:
chrome.notifications.create(getNotificationId(), {
title: 'GCM Message',
iconUrl: 'gcm_128.png',
type: 'basic',
message: messageString
}, function() {});
// Center window on screen.
var screenWidth = screen.availWidth;
var screenHeight = screen.availHeight;
var width = 500;
var height = 300;
chrome.app.window.create('register.html', {
id: "helloWorldID",
outerBounds: {
width: width,
height: height,
left: Math.round((screenWidth-width)/2),
top: Math.round((screenHeight-height)/2)
}
});
}
Upvotes: 1
Views: 349
Reputation: 1029
You cannot start chrome directly from the code that receives the push message. However once the notification is clicked you can use http://www.w3.org/TR/service-workers/#client-focus to see if there is already a window opened or http://www.w3.org/TR/service-workers/#client-navigate to open a new window.
https://tests.peter.sh/notification-generator/ is a good sample site where you can play with the different options.
Upvotes: 1
Reputation: 3773
This is my GCM Listener for message event: chrome.gcm.onMessage.addListener(messageReceived);
It calls messageReceived function and in this function I get message, pop notification on the screen, and open/create the window:
chrome.notifications.create(getNotificationId(), {
title: 'GCM Message',
iconUrl: 'gcm_128.png',
type: 'basic',
message: messageString
}, function() {});
// Center window on screen.
var screenWidth = screen.availWidth;
var screenHeight = screen.availHeight;
var width = 500;
var height = 300;
chrome.app.window.create('register.html', {
id: "helloWorldID",
outerBounds: {
width: width,
height: height,
left: Math.round((screenWidth-width)/2),
top: Math.round((screenHeight-height)/2)
}
});
}
Upvotes: 0