我正在使用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>
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'
有人能说出什么锣错了吗?谢谢..
答案 0 :(得分: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.