UPDATE機器人 - 火力地堡通知推送通知不工作
不幸的是,我並沒有解決這個問題,我所做的就是創建一個新的項目。幸運的是,我的新項目有效。有一件事我從新創建的項目中注意到 ,當我發送通知 消息時,有些消息沒有到達我的設備。所以我認爲它的我的 互聯網連接問題(我的想法只)。
我試圖使用Firebase實現推送通知的基本接收。我根據這個教程:
https://www.codementor.io/android/tutorial/send-push-notifications-to-android-with-firebase
我的問題是我根本不接收任何消息。我使用Firebase控制檯發送了超過5封郵件,但仍然沒有任何反應。
這是我實現FirebaseInstanceIdService的:
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
public static final String debugTag = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token in logcat
Log.e(debugTag, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
我FirebaseMessagingService實現:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String debugTag = "MyFirebaseApp";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(debugTag, "Notification Received!");
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.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)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
我的清單文件:
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".messagingservice.MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".tokenservice.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
我在做什麼錯?或者我缺乏什麼實施?
我真的需要這個幫助。任何幫助將不勝感激。非常感謝你!
您是否在移動設備的系統托盤中收到通知?或者只是'onMessageReceived'方法沒有被調用? – GeorgeLBA
你是怎麼試過的?我的意思是當應用程序在前臺或後臺? –
@Chris Palma是否在模塊級目錄中包含了google-services.json文件? –