2017-01-24 36 views
1

我正在使用最新的GCM推送通知插件與科爾多瓦。通知運行良好,但我希望應用程序在用戶收到推送通知時到達前臺。當接收到GCM推送通知(科爾多瓦)時,將應用程序置於前臺

我已經嘗試了很多地方的幾種解決方案,但是我仍然無法使應用程序在收到通知時到達前臺。

似乎有人設法這裏做類似的事情,但無論我做什麼我不能複製成功:

Bring cordova application to foreground when push arrives

爲GCMIntentService.java在他的問題的代碼是什麼可用的不同現在。

我該如何得到這個工作?

+0

仍在使用'GCM'? 'GCM'已棄用。使用'FCM' – Piyush

+0

如果我使用FCM,這個功能可能嗎? –

回答

0

在GCMIntentservice.java文件的onMessageReceived方法中包含以下行。

@Override 
    public void onMessageReceived(String from, Bundle extras) { 
     Log.d(LOG_TAG, "onMessage - from: " + from); 

     if (extras != null) { 
      Context applicationContext = getApplicationContext(); 

      SharedPreferences prefs = applicationContext.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); 
      boolean forceShow = prefs.getBoolean(FORCE_SHOW, false); 
      boolean clearBadge = prefs.getBoolean(CLEAR_BADGE, false); 

      extras = normalizeExtras(applicationContext, extras); 

      if (clearBadge) { 
       PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0); 
      } 

      // if we are in the foreground and forceShow is `false` only send data 
      if (!forceShow && PushPlugin.isInForeground()) { 
       Log.d(LOG_TAG, "foreground"); 
       extras.putBoolean(FOREGROUND, true); 
       extras.putBoolean(COLDSTART, false); 
       PushPlugin.sendExtras(extras); 
      } 
      // if we are in the foreground and forceShow is `true`, force show the notification if the data has at least a message or title 
      else if (forceShow && PushPlugin.isInForeground()) { 
       Log.d(LOG_TAG, "foreground force"); 
       extras.putBoolean(FOREGROUND, true); 
       extras.putBoolean(COLDSTART, false); 

       showNotificationIfPossible(applicationContext, extras); 
      } 
      // if we are not in the foreground always send notification if the data has at least a message or title 
      else { 
       Log.d(LOG_TAG, "background"); 
       extras.putBoolean(FOREGROUND, false); 

       Intent wintent = new Intent(context, MainActivity.class); 
      wintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(wintent); 

       extras.putBoolean(COLDSTART, PushPlugin.isActive()); 

       showNotificationIfPossible(applicationContext, extras); 
      } 
     } 
    } 

並確保您導入MainActivity.java

+0

感謝您的回覆。我剛纔做了,通過輸入代碼「import MainActivity;」導入MainActivity。雖然它仍然沒有把應用程序放在前面......只是通知標準推送消息的推送。任何其他想法? –

相關問題