2016-08-31 5 views
1

Ich baue eine Hybrid-Android-App mit Phonegap, HTML5, JavaScript und IDE (Eclipse).Wenn die App beendet wurde oder der Hintergrund implementiert wurde, startet die Push-Benachrichtigung nicht

Diese App ist die Beispielanwendung für die Push-Benachrichtigung. Die Push-Benachrichtigung basiert auf dem Java-Server.

Wenn die App gestartet wird, wird die Push-Benachrichtigung normalerweise ausgeführt und wenn die App beendet wird oder im Hintergrund gestartet wird, wird die Push-Benachrichtigung nicht geöffnet.

Auch wenn es funktioniert, und die App nicht ausgeführt wird, kann die Push-Benachrichtigung erhalten werden?

[index.html - In Hybrid-App Projekt]

<!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() - In Spring MVC Web Project]

@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 } 
 
}

< ---- Aktualisierte Inhalte ---->

Flag konnte gemäß @ Mr.Rebot Antwort hinzugefügt werden und die Informationen für wurde gesucht.

Intent intent = new Intent("What does insert in here?"); 
 
intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES); 
 
sendBroadcast(intent);

Vor meiner MainActivity hinzugefügt werden können Code?

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

Antwort

1

(Für Android) Es kann wegen einiger Änderungen von Android Start sein 3.1, wo Startverhalten steuert, wenn eine Anwendung beendet wird gestoppt Empfangen einer Benachrichtigung als "Apps, die sich im angehaltenen Status befinden, empfangen keine Broadcast-Intents" Überprüfen Sie diese related SO question und GCM push notification.

Beachten Sie, dass das System allen Broadcast-Absichten FLAG_EXCLUDE_STOPPED_PACKAGES hinzufügt. Dies verhindert, dass Broadcasts von Hintergrunddiensten versehentlich oder unnötig Komponenten von angehaltenen Anwendungen starten. Ein Hintergrunddienst oder eine Anwendung kann dieses Verhalten überschreiben, indem das Flag FLAG_INCLUDE_STOPPED_PACKAGES Broadcast-Intents hinzufügt, die gestoppte Anwendungen aktivieren sollen.

Anwendungen werden gestoppt, wenn sie zum ersten Mal installiert werden, aber noch nicht gestartet wurden und wenn sie vom Benutzer manuell beendet werden (unter Anwendungen verwalten).

Hoffe, diese Informationen hilft!

+0

Vielen Dank für Ihre Hilfe. Ich habe deine Antwort verstanden, aber da war der Teil, den ich nicht gut kenne.Da der Inhalt, den er fragen möchte, lang war und er sich erneut dem Körper aneignete, wurde die Bestätigung gefragt. – eastglow

Verwandte Themen