我正在尝试按照开发者网站的指南将 Firebase 推送通知合并到我现有的 android 应用程序中。
成功构建应用程序后,如何在不将应用程序发布到 Playstore 的情况下测试 Firebase 通知是否正常工作?
相关文件如下所示:
服务/FirebaseMessagingService.java
public class FirebaseNotificationService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
String messageBody = remoteMessage.getNotification().getBody();
Intent intent = new Intent(this, NavActivity.class);
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)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
应用程序清单.xml
<service android:name="services.FirebaseNotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
构建.gradle
dependencies {
compile 'com.google.firebase:firebase-messaging:9.2.1'
}