2017-05-29 345 views
0

我想通過自己的示例代碼運行火力地堡工作,火力地堡通知不會從火力地堡控制檯

我加入谷歌與services.json用正確的軟件包名稱,

,我想登錄到Firebase控制檯,並根據基於令牌「進入應用程序」的應用程序發送消息。

當應用程序處於後臺或應用程序處於前臺或完全關閉時,我無法在手機端獲得通知。

我在做什麼錯在這裏?

這裏是我的MyFirebaseInsteanceILService

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

    private static final String TAG = "MyFirebaseIIDService"; 

    /** 
    * Called if InstanceID token is updated. This may occur if the security of 
    * the previous token had been compromised. Note that this is called when the InstanceID token 
    * is initially generated so this is where you would retrieve the token. 
    */ 
    // [START refresh_token] 
    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG, "Refreshed token: " + refreshedToken); 

     // If you want to send messages to this application instance or 
     // manage this apps subscriptions on the server side, send the 
     // Instance ID token to your app server. 
     sendRegistrationToServer(refreshedToken); 
    } 
    // [END refresh_token] 

    /** 
    * Persist token to third-party servers. 
    * 
    * Modify this method to associate the user's FCM InstanceID token with any server-side account 
    * maintained by your application. 
    * 
    * @param token The new token. 
    */ 
    private void sendRegistrationToServer(String token) { 
     // TODO: Implement this method to send token to your app server. 
    } 
} 

這裏是我的MyFirebase消息服務

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // [START_EXCLUDE] 
     // There are two types of messages data messages and notification messages. Data messages are handled 
     // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
     // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
     // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
     // When the user taps on the notification they are returned to the app. Messages containing both notification 
     // and data payloads are treated as notification messages. The Firebase console always sends notification 

     // [END_EXCLUDE] 

     // TODO(developer): Handle FCM messages here. 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 

      if (/* Check if data needs to be processed by long running job */ true) { 
       // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher. 
       scheduleJob(); 
      } else { 
       // Handle message within 10 seconds 
       handleNow(); 
      } 

     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 

     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 
    } 
    // [END receive_message] 

    /** 
    * Schedule a job using FirebaseJobDispatcher. 
    */ 
    private void scheduleJob() { 
     // [START dispatch_job] 
     FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this)); 
     Job myJob = dispatcher.newJobBuilder() 
       .setService(MyJobService.class) 
       .setTag("my-job-tag") 
       .build(); 
     dispatcher.schedule(myJob); 
     // [END dispatch_job] 
    } 

    /** 
    * Handle time allotted to BroadcastReceivers. 
    */ 
    private void handleNow() { 
     Log.d(TAG, "Short lived task is done."); 
    } 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     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_stat_ic_notification) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 

的build.gradle(項目)

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
     mavenLocal() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.3.2' 
     classpath 'com.google.gms:google-services:3.1.0' 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
     mavenLocal() 

    } 
} 

的build.gradle(模塊)

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion '25.0.3' 

    defaultConfig { 
     applicationId "com.google.firebase.quickstart.fcm" 
     minSdkVersion 14 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 

    buildTypes { 
     release { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

    packagingOptions { 
     exclude 'LICENSE.txt' 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:25.3.1' 

    compile 'com.google.firebase:firebase-messaging:10.2.6' 
    compile 'com.firebase:firebase-jobdispatcher:0.5.2' 

    // Testing dependencies 
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 
    androidTestCompile 'com.android.support.test:runner:0.5' 
    androidTestCompile 'com.android.support:support-annotations:25.3.1' 
} 

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

清單:

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

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

     <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
     <uses-permission android:name="android.permission.WAKE_LOCK" /> 

     <!-- [START fcm_default_icon] --> 

     <meta-data 
      android:name="com.google.firebase.messaging.default_notification_icon" 
      android:resource="@drawable/ic_stat_ic_notification" /> 

     <meta-data 
      android:name="com.google.firebase.messaging.default_notification_color" 
      android:resource="@color/colorAccent" /> 
     <!-- [END fcm_default_icon] --> 
     <activity 
      android:name="com.google.firebase.quickstart.fcm.MainActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 

     <!-- [START firebase_service] --> 
     <service 
      android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 
     <!-- [END firebase_service] --> 
     <!-- [START firebase_iid_service] --> 
     <service 
      android:name=".MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service> 
     <!-- [END firebase_iid_service] --> 
     <service android:name=".MyJobService" 
       android:exported="false"> 
      <intent-filter> 
       <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/> 
      </intent-filter> 
     </service> 
    </application> 

</manifest> 
+0

@Chief Madog請檢查我的答案。 –

+0

@RatilalChopda我做了,它仍然沒有得到任何通知 –

+0

檢查ans我的朋友它在我的情況下工作詢問我的情況下任何查詢 –

回答

0

您可以在清單文件中添加INTERNET PERMISION

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

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

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



    <!-- [START fcm_default_icon] --> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@drawable/ic_stat_ic_notification" /> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_color" 
     android:resource="@color/colorAccent" /> 
    <!-- [END fcm_default_icon] --> 
    <activity 
     android:name="com.google.firebase.quickstart.fcm.MainActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 

    <!-- [START firebase_service] --> 
    <service 
     android:name=".MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_service] --> 
    <!-- [START firebase_iid_service] --> 
    <service 
     android:name=".MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_iid_service] --> 
    <service android:name=".MyJobService" 
      android:exported="false"> 
     <intent-filter> 
      <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/> 
     </intent-filter> 
    </service> 
</application> 

</manifest> 
+0

如果我添加權限, –

+0

@ChiefMadog請檢查已更新的清單文件,但它仍然無助於改變。 –

+0

@ChiefMadog permision總是在應用程序標籤前添加。 –

0

你忘了寫

​​

在menifest.xml 和

把此行中onMessageReceived()方法

sendNotification(remoteMessage.getNotification().getBody()); 

確保您的註冊ID不是空白

+0

它仍然沒有幫助什麼改變,如果我添加權限,註冊ID,你的意思是令牌,它不是空白。 thnx! –

+0

kk我會檢查等候 –

+0

看到我更新的答案bro –

0

試試這個創建firebasemessageservice類

public class FirebaseMessageService extends FirebaseMessagingService { 
public String TAG = "FireBaseMassaging"; 

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

    Log.e(TAG, "From: " + remoteMessage.getFrom()); 

    FirebaseInstanceId.getInstance().getToken(); 
    Log.e(TAG, "Your Token " + FirebaseInstanceId.getInstance().getToken()); 
    sendNotification(remoteMessage.getNotification().getBody()); 
} 




private void sendNotification(String body) { 
    Intent intent = new Intent(this, HomeClass.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Log.e(TAG, "Message: " + body); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.logo) 
      .setContentTitle("RedFox") 
      .setContentText(body) 
      .setDefaults(-1) 
      .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) 
      .setAutoCancel(true) 
      .setSound(Settings.System.DEFAULT_NOTIFICATION_URI) 
      .setContentIntent(pendingIntent); 

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

創建FirebaseServiceId類

public class FirebaseServiceId extends FirebaseInstanceIdService { 
public static final String TAG = "FirebaseId"; 

@Override 
public void onTokenRefresh() { 
    super.onTokenRefresh(); 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.e(TAG, "Refresh token is: " + refreshedToken); 
    sendRegistrationToServer(refreshedToken); 

} 

private void sendRegistrationToServer(String token) { 
    //You can implement this method to store the token on your server 
    //Not required for current project 
    Log.e(TAG, "Token2:" + token); 
}} 

在manifiest文件中添加此

<uses-permission android:name="android.permission.INTERNET" /> 
<service android:name=".FirebaseServiceId"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".FirebaseMessageService" 
     android:stopWithTask="false"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
     </intent-filter> 
    </service>