更改包名称后 FCM 通知不起作用

Posted

技术标签:

【中文标题】更改包名称后 FCM 通知不起作用【英文标题】:FCM notifications not working after changing the package name 【发布时间】:2016-10-28 05:58:56 【问题描述】:

我正在使用 FCM 发送通知。在我更改包名称之前,它运行良好。

当我更改包名称时,我在 firebase 控制台中创建了新项目,获得了新密钥并从 php 发送带有新密钥的通知,但我没有收到通知。当我从firebase测试它的工作时。

当我尝试调试时,我发现 onMessageReceived 没有被调用,尽管它在前台。

消息服务:

    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'

谁能告诉我怎么了?谢谢。。

【问题讨论】:

发布完整的 gradle 和 manifest? 你能仔细检查 Firebase 控制台中的包名和 gradle 和 manifest 是否相同吗? 是的,检查了很多次。因为它可以在 firebase 上运行,所以应该没问题。。添加了清单和 gradle .. :-( @Raghavendra 可能是个愚蠢的问题。在您的旧实现中,清单声明是相同的吗?我的意思是 ? 是的,是一样的。我需要在服务器中使用哪个 api 密钥?来自firebase控制台的Web api密钥或当我生成新的json文件时,它在api_key中显示current_key,因为我检查了两者似乎不同。应该使用哪个键? @Raghavendra 【参考方案1】:

您的 json (google-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.

【讨论】:

是的,我在 app 文件夹中添加了新的 json 文件。 @牛轧糖情人 你能在 .json 文件中打开并重新确认你的 package_name 吗? "package_name": "com.weberz" 这是 json 文件中的包名。 @牛轧糖情人 我需要在服务器中使用哪个 api 密钥?来自firebase控制台的Web api密钥或当我生成新的json文件时,它在api_key中显示current_key,因为我检查了两者似乎不同。应该使用哪个键? @牛轧糖情人 您的客户端密钥和服务器密钥将不同。

以上是关于更改包名称后 FCM 通知不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用离子电容器的 FCM 推送通知在 IOS 中不起作用

突然 fcm 通知在某些 iOS 设备上不起作用

FCM 后台通知在 iOS 中不起作用

FCM 链接在桌面通知中不起作用

FCM 推送通知在 Android 4.4 上不起作用

向 FCM 服务器发送推送通知不起作用