2016-08-31 16 views
1

Phonegap、HTML5、JavaScript、IDE(eclipse)を使用してハイブリッドAndroidアプリを構築しています。アプリが終了したり、バックグラウンドが実装されている場合、プッシュ通知は起動しません

このアプリはプッシュ通知のサンプルアプリです。プッシュ通知は、Javaサーバーに基づいて作成されました。

アプリが起動されると、プッシュ通知が正常に動作し、アプリが終了するか、バックグラウンドで起動されるとプッシュ通知は開かれません。

どうやってアプリが実行されていなくても、プッシュ通知を受け取れるのですか? 【のindex.html - ハイブリッドアプリプロジェクトで]

-

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

<!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>

【春MVC WebプロジェクトでGcmTestServer()]

< ----更新内容---->

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

答えて

1

(アンドロイドについて)それがため起動が停止アプリケーションが停止する動作を制御アンドロイド3.1から出発して、いくつかの変更であってもよいです「停止状態のAppsはブロードキャストインテントを受信しない」という通知を受け取ります。 related SO questionGCM push notificationを確認してください。

すべてのブロードキャストインテントにFLAG_EXCLUDE_STOPPED_PACKAGESが追加されます。バックグラウンドサービスからのブロードキャストが誤って、または不必要に停止したアプリケーションのコンポーネントを起動しないようにするためです。バックグラウンドサービスまたはアプリケーションは、停止したアプリケーションをアクティブにする必要があるブロードキャストインテントにFLAG_INCLUDE_STOPPED_PACKAGESフラグを追加することによって、この動作を無効にすることができます。

アプリケーションは、最初にインストールされたときに停止状態ですが、まだ起動されていない場合や、ユーザーによって手動で停止された場合(アプリケーションの管理)

この情報が役立ちますようお願いいたします。

+0

ありがとうございます。私はあなたの答えを理解しましたが、よく分からない部分がありました。聞きたい内容が長くて体に再び改訂されたので、確認が求められます。 – eastglow

関連する問題