2017-08-29 104 views
1

我正在開發一個應用程序,通過Firebase發送通知。我按照幾個教程,其中的想法是當我按下通知時打開一個活動。只要應用程序處於打開狀態,此操作就可以正常工作,但如果它已關閉或「最小化」並按下通知,它將從默認活動打開,在我的情況下這將是SplashScreen,我真的不解釋它爲什麼不起作用。有什麼可以推薦的嗎?在Android Studio中關閉應用程序時,推送通知無法正常工作

以下是我們知道的類MyFirebaseMessagingService。正如你所看到的,當我按下通知時,它會帶我到NotificationActivity.class,它會......但只有當應用程序是打開的。

package com.asecum.com.campussicapp; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.content.ContextCompat; 
import android.util.Log; 

import com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 

import org.json.JSONException; 
import org.json.JSONObject; 

/** 
* Created by asecum5 on 24/08/17. 
*/ 

public class Firebase_NotificationService extends FirebaseMessagingService { 

    private static final String TAG = "NOTICIAS"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 

     /*String from = remoteMessage.getFrom(); 
     Log.d(TAG, "Mensaje recibido de: " + from); 

     if(remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Notification: " + remoteMessage.getNotification().getBody()); 

      showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); 
     } 
     if(remoteMessage.getData().size()>0){ 
      Log.d(TAG, "Data: " + remoteMessage.getData()); 
     }*/ 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Data: " + remoteMessage.getData()); 
      try { 
       JSONObject data = new JSONObject(remoteMessage.getData()); 
       String jsonMessage = data.getString("extra_information"); 
       Log.d(TAG, "onMessageReceived: \n" + "Extra information: " + jsonMessage); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
     if (remoteMessage.getNotification() != null) { 
      String title = remoteMessage.getNotification().getTitle(); 
      String message = remoteMessage.getNotification().getBody(); 
      showNotification(title, message); 
     } 
    } 
    private void showNotification(String title, String body) { 
     Intent intent = new Intent(this, NotificationActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

     Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_home_black_24dp) 
       .setContentTitle(title) 
       .setContentText(body) 
       .setAutoCancel(true) 
       .setVibrate(new long[]{100, 250, 100, 250, 100, 250}) 
       .setSound(soundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 
    } 
} 

下一個是清單...可能是我忘記了一些權限?

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.asecum.com.campussicapp"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 


     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="MAINACTIVITY" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".SplashScreen" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

     </activity> 


     <activity 
      android:name=".NotificationActivity"> 
      <intent-filter> 
       <action android:name="NOTIFICATIONACTIVITY" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 


     <service 
      android:name=".Firebase_InstanceIdService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

     <service 
      android:name=".Firebase_NotificationService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 

     <meta-data 
      android:name="com.google.firebase.messaging.default_notification_color" 
      android:resource="@color/colorAccent" /> 
    </application> 

</manifest> 

這是我在gradle

模塊的的build.gradle

dependencies { 
    ... 

    compile 'com.google.firebase:firebase-core:10.2.6' 
    compile 'com.google.firebase:firebase-messaging:10.2.6' 
    testCompile 'junit:junit:4.12' 
} 

apply plugin: `com.google.gms.google-services` 

項目的增加的build.gradle

dependencies { 
    ... 
classpath 'com.google.gms:google-services:3.1.0' 
} 

回答

0

我認爲這是由the difference between "data" style cloud messages and "notification" style cloud messages in Firebase.

引起

對於通知風格信息:

FCM自動顯示給最終用戶的客戶端應用程序的代表 設備上的消息。

另請注意,firebase控制檯只能發送消息的通知樣式。

難道是這個通知樣式雲消息到達,並正在由android系統自動處理,而不是由您的應用程序(當應用程序關閉或在後臺)?

如果是這種情況,系統將在用戶單擊通知時打開應用程序的默認活動(啓動畫面)。

作爲替代,您可以send a "data" style cloud message這將由您的應用程序而不是由系統處理。然後,當用戶點擊通知時,它將執行.Firebase_NotificationService中定義的代碼。您可以使用這樣的腳本通過Google的服務器將數據樣式消息發送到特定的應用程序實例編號:

curl -X POST \ 
--Header "Authorization: key=<get authorization key from Firebase console - Project Settings - Web API Key>」 \ 
--Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send \ 
-d " \ 
    { \ 
     \"to\」:\」<get id for your particular android device app instance from Firebase Instance ID service in your app>\」, \ 
     \"data\": \ 
     { \ 
      \"title\":\" test title here \", \ 
      \"body\":\" test body here \" 
     }, \ 
     \"priority\": \"normal\" \ 
    }" 

請注意,JSON中沒有「通知」塊。

又見thisthis

+0

謝謝你幫我...我閱讀有關數據,並通知和火力地堡文檔之間的問題太多。我看到發生這種情況是因爲Android系統默認具有該操作。你知道我怎樣才能發送數據風格的雲消息?我必須在Firebase控制檯或Android Studio中執行此操作嗎? 預先感謝您 –

+0

發送數據樣式消息的一種方法是從命令行使用curl:https://stackoverflow.com/a/37376757/3482621 –

+0

另一種方法是使用Java程序:https:// stackoverflow .COM /問題/ 37426542 /火力點雲的消息通知,從-Java的INSTEAD-OF-火力控制檯 –

相關問題