1

我正在使用FCM發送通知。直到我更改了包名後,它纔開始工作。更改包名稱後FCM通知不起作用

由於我更改了軟件包名稱,我在Firebase控制檯中創建了新項目,獲得了新的密鑰並使用PHP中的新密鑰發送通知,但我沒有收到通知。當我從Firebase測試它的工作。

正如我試圖調試我發現onMessageReceived不會被調用,雖然是在前臺。

MessageService:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 
    private String mUserId; 
    private Boolean mUpdateNotification; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     //Displaying data in log 
     //It is optional 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     String clickAction = remoteMessage.getNotification().getClickAction(); 

     mUserId = remoteMessage.getData().get("user_id"); 

     String title = remoteMessage.getNotification().getTitle(); 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getNotification().getBody(),clickAction,title); 
    } 

    //This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(String messageBody,String clickAction,String title) { 

     mUpdateNotification = true; 

     Intent intent = new Intent(clickAction); 

     intent.putExtra("userId",mUserId); 
     intent.putExtra("updateNotification",mUpdateNotification); 

     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); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.contacts_icon) 
       .setContentTitle(title) 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

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

} 

PHP:

public function sendPush($text, $tokens, $apiKey) 
{ 

    $notification = array(
     "title" => "You got an invitation.", 
     "text" => $text, 
     "click_action" => "OPEN_ACTIVITY_1" 

    ); 

    $msg = array 
    (
     'message' => $text, 
     'title' => 'You got an invitation.', 
    ); 
    $fields = array 
    (
     'to' => $tokens, 
     'data' => $msg, 
     'notification' => $notification 
    ); 

    $headers = array 
    (
     'Authorization: key=' . $apiKey, 
     'Content-Type: application/json' 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    $result = curl_exec($ch); 
    // echo($result); 
    // return $result; 
    curl_close($ch); 
} 

清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.weberz"> 

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 

    <application 
     android:name="android.support.multidex.MultiDexApplication" 
     android:allowBackup="true" 
     android:icon="@drawable/contacts_icon" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".Activities.MainActivity" /> 

     <service android:name=".helper.MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 
     <service 
      android:name=".helper.MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" 
        android:enabled="true"/> 
      </intent-filter> 
     </service> 

     <activity 
      android:name=".Activities.RegisterActivity" 
      android:label="@string/title_activity_main2" 
      android:theme="@style/AppTheme" 
      android:windowSoftInputMode="stateHidden" /> 
     <activity android:name=".Activities.DetailViewActivity"> 
      <intent-filter> 
       <action android:name="OPEN_ACTIVITY_2" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Activities.ProfileActivity" 
      android:windowSoftInputMode="stateHidden" /> 
     <activity 
      android:name=".Activities.LoginActivity" 
      android:label="@string/title_activity_login" 
      android:theme="@style/AppTheme" /> 
     <activity android:name=".Activities.StartUpActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <service 
      android:name=".helper.MessageService" 
      android:enabled="true" /> 

     <activity 
      android:name=".Activities.ForgotPasswordActivity" 
      android:label="@string/title_activity_forgot_password" 
      android:theme="@style/AppTheme" /> 
     <activity android:name=".Activities.InviteContactsActivity" /> 
     <activity 
      android:name=".Activities.PendingInvitesActivity" 
      android:label="@string/title_activity_pending_invites" 
      android:theme="@style/AppTheme"> 
      <intent-filter> 
       <action android:name="OPEN_ACTIVITY_1" /> 

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

     <receiver android:name=".helper.MessageService$SmsSentReceiver" /> 
     <receiver android:name=".helper.MessageService$SmsDeliveredReceiver" /> 

     <activity android:name=".Activities.PreferencesActivity" 
      android:theme="@style/PreferencesTheme"/> 

    </application> 

</manifest> 

搖籃:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 24 
    buildToolsVersion "24.0.1" 

    defaultConfig { 
     applicationId "com.weberz" 
     minSdkVersion 14 
     targetSdkVersion 24 
     versionCode 1 
     multiDexEnabled true 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 

      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:24.2.1' 
    compile 'com.android.support:recyclerview-v7:24.2.1' 
    compile 'de.hdodenhof:circleimageview:2.1.0' 
    compile 'com.android.support:design:24.2.1' 
    compile 'com.squareup.picasso:picasso:2.5.2' 
    compile 'com.google.code.gson:gson:2.2.4' 
    compile 'com.google.android.gms:play-services:9.6.1' 
    compile 'com.google.firebase:firebase-messaging:9.6.1' 
    compile 'com.firebase:firebase-client-android:2.5.1' 
    compile 'com.android.support:multidex:1.0.1' 
    compile 'com.google.firebase:firebase-core:9.6.1' 
    compile 'com.afollestad.material-dialogs:commons:0.9.0.1' 

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

任何人都可以告訴什麼是功錯嗎?謝謝。

+0

發佈完整的gradle和manifest? – Raghavendra

+0

您可以仔細檢查Firebase控制檯中的軟件包名稱,並且gradle和manifest是相同的。 – Raghavendra

+0

是的檢查很多次..因爲它從firebase工作,那麼它不應該是一個問題..添加清單和gradle .. :-(@Raghavendra – Sid

回答

1

您的JSON(谷歌service.json)文件需要被重新提取其保存在您的應用程序文件夾

爲了讓您的Web API密鑰...

1. Go to Firebase Console 
2. Select Your Project … then click on the gear icon next to project name and click on Project settings then move on to CLOUD MESSAGING tab. 
+0

是的我在應用程序文件夾中添加了新的json文件@Nougat Lover – Sid

+0

你可以在.json文件中打開並重新確認你的package_name? –

+0

「package_name」:「com。 weberz「這是json文件中的包名稱@Nougat Lover – Sid