我正在使用Phonegap,HTML5,JavaScript和IDE(eclipse)構建混合Android應用程序。當應用程序被終止或背景實施時,推送通知不會啓動
此應用程序是推送通知的示例應用程序。基於Java服務器的推送通知。
但是,當應用程序啓動時,推送通知正常運行,當應用程序終止或在後臺啓動時,推送通知不會打開。
即使它確實如此,應用程序未執行,是否可以獲取推送通知?
[index.html的 - 在混合應用項目]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
\t <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
\t <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
\t <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
\t \t <script type="text/javascript">
\t \t /**
\t \t * tokenHandler
\t \t *
\t \t * @param result \t \t
\t \t */
\t \t function tokenHandler(result){
\t \t console.log('deviceToken:' + result);
\t \t }
\t \t
\t \t /**
\t \t * errorHandler
\t \t *
\t \t * @param err
\t \t */
\t \t function errorHandler(err){
\t \t console.log('error:' + err);
\t \t }
\t \t
\t \t /**
\t \t * successHandler
\t \t *
\t \t * @param result
\t \t */
\t \t function successHandler(result){
\t \t console.log('result:'+result);
\t \t }
\t \t
\t \t /**
\t \t * onNotificationAPN
\t \t *
\t \t * @param event
\t \t */
\t \t function onNotificationAPN (event){
\t \t if (event.alert){
\t \t navigator.notification.alert(event.alert);
\t \t }
\t \t
\t \t if (event.sound){
\t \t var snd = new Media(event.sound);
\t \t snd.play();
\t \t }
\t \t
\t \t if (event.badge){
\t \t window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
\t \t }
\t \t }
\t \t
\t \t /**
\t \t * onNotificationGCM
\t \t *
\t \t * @param e
\t \t */
\t \t function onNotificationGCM (e){
\t \t \t switch (e.event) {
\t \t \t \t case 'registered':
\t \t \t \t { \t
\t \t \t \t \t console.log(e.regid);
\t \t \t \t \t $('#token').val(e.regid);
\t \t \t \t \t break;
\t \t \t \t }
\t \t \t \t case 'message':
\t \t \t \t {
\t \t \t \t \t if (e.foreground){
\t \t \t \t \t \t var soundfile = e.soundname || e.payload.sound;
\t \t \t \t \t \t var my_media = new Media("/android_asset/www/" + soundfile);
\t \t \t \t \t \t my_media.play();
\t \t \t \t \t }
\t \t \t \t \t else {
\t \t \t \t \t \t if (e.coldstart) {
\t \t \t \t \t \t \t console.log("Case : coldstart");
\t \t \t \t \t \t }
\t \t \t \t \t \t else {
\t \t \t \t \t \t \t console.log("background");
\t \t \t \t \t \t }
\t \t \t \t \t }
\t \t \t \t \t console.log(e.payload.title);
\t \t \t \t \t navigator.notification.alert(e.payload.title);
\t \t \t \t }
\t \t \t \t break;
\t \t \t \t case 'error':
\t \t \t \t \t console.log('error:' + e.msg);
\t \t \t \t \t break;
\t \t \t \t case 'default':
\t \t \t \t \t console.log('The event which it cannot know');
\t \t \t \t \t break;
\t \t \t }
\t \t }
\t \t document.addEventListener("deviceready", function(){
\t \t \t console.log(device.platform);
\t \t \t if(device.platform.toUpperCase() == 'ANDROID'){
\t \t \t \t window.plugins.pushNotification.register(successHandler,errorHandler, {
\t \t \t \t \t "senderID" : "My sender ID", //
\t \t \t \t \t "ecb" : "onNotificationGCM" //
\t \t \t \t });
\t \t \t }
\t \t \t else
\t \t \t {
\t \t \t \t window.plugins.pushNotification.register(tokenHandler, errorHandler, {
\t \t \t \t "badge":"true",
\t \t \t \t "sound":"true",
\t \t \t \t "alert":"true",
\t \t \t \t "ecb": "onNotificationAPN"
\t \t \t \t });
\t \t \t }
\t \t });
\t \t </script>
\t \t
\t <script type="text/javascript">
\t \t function insert(){
\t \t \t var formData = $("#memForm").serialize();
\t \t \t alert(formData);
\t \t \t $.ajax({
\t \t \t \t type:"POST",
\t \t \t \t data:formData,
\t \t \t \t url:"http://192.168.0.26:9102/insertMember.do",
\t \t \t \t success:function(data){
\t \t \t \t \t alert("Success");
\t \t \t \t \t location.reload();
\t \t \t \t },
\t \t \t \t error:function(request,status,error){
\t \t \t alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
\t \t \t \t }
\t \t \t });
\t \t }
\t \t
\t \t function pushTest(){
\t \t \t $.ajax({
\t \t \t \t type:"POST",
\t \t \t \t url:"http://192.168.0.26:9102/pushTest.do",
\t \t \t \t success:function(data){
\t \t \t \t \t alert("push Test!");
\t \t \t \t },
\t \t \t \t error:function(request,status,error){
\t \t \t alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
\t \t \t \t }
\t \t \t });
\t \t }
\t </script>
<title>Hello World</title>
</head>
<body>
<div data-role="page">
\t \t \t <div data-role="header" data-position="fixed">
\t \t \t \t <h1>hairShare</h1>
\t \t \t </div>
\t \t \t <form id="memForm">
\t \t \t \t <div role="main" class="ui-content" data-inset="false">
\t \t \t \t \t <input type="hidden" name="token" id="token" value="">
\t \t \t \t \t <input type="text" name="id">
\t \t \t \t \t <input type="password" name="pw">
\t \t \t \t \t <input type="text" name="email">
\t \t \t \t \t <input type="button" onclick="insert()" value="Submit">
\t \t \t \t \t <input type="button" onclick="pushTest()" value="Push">
\t \t \t \t </div>
\t \t \t </form>
</div>
</body>
</html>
[GcmTestServer() - 在Spring MVC的Web項目]
@Controller
public class GcmTestServer {
\t @RequestMapping(value = "/pushTest.do", method = {RequestMethod.GET, RequestMethod.POST})
\t @ResponseBody
\t public void pushTest() throws Exception {
\t \t try {
\t \t \t // TODO Auto-generated method stub
\t \t \t Sender sender = new Sender("AIzaSyBiJUHJ47I_eAZehDC0lx-1k5FSpcQPqyM"); // 서버 API Key 입력
\t \t \t String regId = "APA91bFDd3srzd8UaaTFvCv4xheJ-WSXsU5eoQY8-veT1TSLQvDyXTtjSfiaw94nZjlj8nIonL6-PNw4lzKX0rb-bju8jnSbsMeQ93LGuvBGUuguOfwhCJ4"; // 단말기 RegID 입력
\t \t \t
\t \t \t String sendTlt = "send Title";
\t String sendMsg = "send Message";
\t \t \t
\t Message message = new Message.Builder()
\t .addData("title", sendTlt)
\t .addData("msg", sendMsg)
\t .build();
\t \t \t List<String> list = new ArrayList<String>();
\t \t \t list.add(regId);
\t \t \t MulticastResult multiResult;
\t \t
\t \t \t multiResult = sender.send(message, list, 5);
\t \t \t if (multiResult != null) {
\t \t \t \t \t List<Result> resultList = multiResult.getResults();
\t \t \t \t \t for (Result result : resultList) {
\t \t \t \t \t \t System.out.println(result.getMessageId());
\t \t \t \t \t }
\t \t \t }
\t \t } catch (IOException e) {
\t \t \t // TODO Auto-generated catch block
\t \t \t e.printStackTrace();
\t \t }
\t }
}
< ----更新內容---->
可以根據@ Mr.Rebot的回答添加標誌,並且搜索到的信息。
Intent intent = new Intent("What does insert in here?");
intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
上面的代碼可以被添加到我的MainActivity?
[MainActivity.java]
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package com.su.project;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import org.apache.cordova.*;
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}
謝謝你的幫助。我明白了你的答案,但是有一部分做得不好。由於它想要詢問的內容很長,並且再次修改爲身體,所以要求確認。 – eastglow