机器人 - 火力地堡通知推送通知不工作
问题描述:
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>
我在做什么错?或者我缺乏什么实施?
我真的需要这个帮助。任何帮助将不胜感激。非常感谢你!
答
不要忘记,我有同样的问题,它是通过定义启用,出口到真正的在我的服务,努力做到像我一样,解决了..
<service android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</aplication>
,然后设置权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
+0
谢谢您的提醒 –
您是否在移动设备的系统托盘中收到通知?或者只是'onMessageReceived'方法没有被调用? – GeorgeLBA
你是怎么试过的?我的意思是当应用程序在前台或后台? –
@Chris Palma是否在模块级目录中包含了google-services.json文件? –