gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:55:09 25 4
gpt4 key购买 nike

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

当我更改包名称时,我在 firebase 控制台中创建了新项目,获取了新 key 并使用来自 php 的新 key 发送通知,但我没有收到通知。当我从 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);
}

list :

<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>

Gradle :

    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'

谁能告诉我哪里出了问题??谢谢..

最佳答案

您的 json (google-service.json) 文件需要重新提取以将其保存在您的应用程序文件夹中

获取您的 Web API key ...

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.

关于php - 更改包名称后 FCM 通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40298904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com