下面我解释了我的示例并给出了最少的代码来帮助理解这个问题。
最后到我的 gradle 文件
apply plugin: 'com.google.gms.google-services'
在我的依赖项 {} 中,我添加了
compile 'com.google.firebase:firebase-messaging:9.2.0'
我在依赖项{}中也有以下内容
compile 'com.google.android.gms:play-services:9.2.0'
compile 'com.google.android.gms:play-services-maps:9.2.0'
compile 'com.google.android.gms:play-services-ads:9.2.0'
compile 'com.google.android.gms:play-services-auth:9.2.0'
compile 'com.google.android.gms:play-services-gcm:9.2.0'
compile 'com.google.android.gms:play-services-analytics:9.2.0'
compile 'com.google.android.gms:play-services-location:9.2.0'
compile 'com.google.maps.android:android-maps-utils:0.4.+'
清单我有
<!-- Google Cloud Messaging -->
<uses-permission
android:name="com.mcruiseon.buseeta.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<service android:name=".support.NotificationIncoming">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".support.NotificationsIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
NotificationIncoming.java 有
public class NotificationIncoming extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(God.LOG_TAG, "From: " + remoteMessage.getFrom());
Log.d(God.LOG_TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MyBookings.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_launcher)
.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());
}
}
在我的 NotificationIDService
public class NotificationsIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(God.LOG_TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
在 Firebase 上,我完成了以下操作
- 创建了一个项目
- 添加了 SHA1 指纹
- 下载 google-services.json 并将其复制到 app 文件夹。
我从与 SHA1 相同的密钥库构建了一个签名应用程序。并运行应用程序。然后我从 Firebase 控制台发送消息。
没运气。我错过了什么?我认为一些非常基本的东西。
编辑
对于所有阅读本文的人,以上内容对我来说非常有效。