2017-07-01 49 views
2

我試圖使用FCM將通知實現到我的應用程序,並且遇到了一些問題。當應用程序處於前臺時,不會出現任何問題,並且按預期顯示通知。但是,如果應用程序在後臺,通知將不會顯示。僅當應用程序處於前臺時纔會收到Android通知

我的清單看起來像這樣:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="xxx"> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

<uses-sdk android:minSdkVersion="19" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <service 
     android:name=".NotificationService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 

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

    <activity android:name=".MainActivity" android:launchMode="singleTask"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <meta-data android:name="com.google.android.gms.games.APP_ID" 
     android:value="@string/app_id" /> 


    <meta-data 
     android:name="io.fabric.ApiKey" 
     android:value="xxx" /> 
    </application> 
    </manifest> 

我的服務類:

package pw.ckies.snakegame; 

import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

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

import java.util.Random; 

public class NotificationService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     String title = "Empty"; 
     String message = "Emptyyy"; 
     if(remoteMessage.getNotification() != null) { 
      if (remoteMessage.getNotification().getTitle() != null) { 
       title = remoteMessage.getNotification().getTitle(); 
      } 


      if (remoteMessage.getNotification().getBody() != null) { 
       message = remoteMessage.getNotification().getBody(); 
      } 
     } 

     Log.e("notification","recieved"); 


     sendNotification(title, message); 
    } 

    private void sendNotification(String title, String message) { 

     Intent intent = new Intent(this, MainActivity.class); 

     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_people_black_24dp) 
       .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), 
         R.mipmap.ic_launcher)) 
       .setContentTitle(title) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(getRequestCode(), notificationBuilder.build()); 
    } 

    private static int getRequestCode() { 
     Random rnd = new Random(); 
     return 100 + rnd.nextInt(900000); 
    } 

} 

而且當通知收到logcat的說:

07-01 23:09 :14.050? W/GCM-DMM:廣播意圖回調:結果=取消forIntent {行動= com.google.android.c2dm.intent.RECEIVE FLG = 0x10000000的PKG = XXX(有演員)}

我不知道是什麼這意味着我搜索了它並發現了許多有相同問題的人。有人說重新安裝Android Studio會解決這個問題,所以我這樣做了,並且沒有任何運氣地更新到2.3.3的最新版本。 我一直在android 7.1.1上測試OnePlus 3。

我build.grade(應用模塊):

... 
    dependencies { 
     compile fileTree(include: ['*.jar'], dir: 'libs') 
     testCompile 'junit:junit:4.12' 
     compile "com.google.android.gms:play-services-games:${gms_library_version}" 
     compile project(':BaseGameUtils') 
     compile 'com.android.support:appcompat-v7:24.0.0' 

     compile 'com.android.support:design:24.0.0' 
     compile 'com.android.support:support-annotations:24.0.0' 
     compile 'com.google.firebase:firebase-ads:11.0.1' 
     compile 'com.google.firebase:firebase-core:11.0.1' 
     compile 'com.google.firebase:firebase-messaging:11.0.1' 
     compile('com.crashlytics.sdk.android:crashlytics:[email protected]') { 
      transitive = true; 
     } 
    } 

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

我的後端是使用下面的數據一個python post請求:

{ 
    "notification": {"title": "Title goes here", "body": "Body goes here" }, 
    "to": token 
} 

我還試圖從儀表板火力發送通知。

有人還建議一個未簽名的APK不會允許我不確定的背景通知?但是因爲我的應用在Play商店中並不存在任何問題。

我已經測試了一切我能想到但我找不到解決方案!這可能是我的電話,也可能只是一個愚蠢的錯誤。任何幫助表示讚賞!

+0

當應用程序在後臺的通知是由系統托盤 – Killer

+0

@Shubham處理是什麼意思呢?它不應該仍然顯示? –

+0

只有當通知有效載荷包含標題和正文(可選)數據密鑰時纔會顯示 – Killer

回答

0

From the firebase docs

當在背景中,應用接收在 通知托盤的通知有效載荷,並且只處理數據有效載荷,當用戶在通知抽頭 。

有人還建議一個未簽名的APK不會允許背景通知,我不知道?但是因爲我的應用在Play商店中並不存在任何問題。

您的應用程序默認使用默認密鑰庫進行簽名。全部取決於在Firebase控制檯中添加到項目設置中的SHA1。

對於生產或調試密鑰庫,你可以從 here

我已經測試了所有我能想到生成SHA1,但我不能找到一個解決辦法! 嘗試發送通知有效載荷包括標題和正文(可選)數據鍵

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "notification" : { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark", 
     "icon" : "myicon" 
    } 
    } 
+0

無論我發送什麼JSON,得到相同的結果...通知,與兩個數據等 –

+0

嘗試添加SHA1爲您的調試版本的應用程序在Firebase控制檯項目設置 – Killer

+0

沒有什麼區別,因爲我一直在嘗試從Play商店發佈版本.. –

0

FirebaseMessagingService的「onMessageReceived()」方法不會被調用時,你的應用程序在後臺或關閉。它發生在你沒有發送任何數據的通知中。您需要通過您的服務器上的數據有效負載發送您需要的任何值。之後,即使應用程序關閉或在後臺,它也可以在每種情況下都能正常工作。

注意:要發送通知中的數據,您需要擁有自己的服務器,Firebase不便利。

檢查此鏈接瞭解詳情:

How to handle notification when app in background in Firebase

+0

發送數據值沒有任何區別。 –

+0

請確保您的服務器正確發送數據有效負載,因爲我告訴您的問題是上述問題中最常見的原因。一定有一些缺失。精確地針對應用程序和服務器進行本教程。它的作用像魅力。 http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ –

相關問題