android - Firebase通知推送通知不起作用 [英] android - Firebase Notification Push Notification not working
问题描述
不幸的是,我没有解决这个问题,创建一个新的项目。幸运的是我的新项目工作。有一件事我从新创建的项目中注意到
,当我发送通知
消息时,有些消息没有到达我的设备。所以我认为它是我的
互联网连接问题(我的想法)。
我试图实现基本接收使用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();
//获取注册令牌
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
//在logcat中显示标记
Log.e(debugTag,刷新标记:+ refreshedToken);
$ b private void sendRegistrationToServer(String token){
//你可以实现这个方法来在你的服务器上存储令牌
// Not当前项目需要
$ b 我的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!);
$ b $ //调用方法来生成通知
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification()。getBody());
//这个方法只产生推送通知
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/>
应用程序$ b $ 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>
< 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>
我做错了什么?或者我缺乏什么实施?
我真的需要这个帮助。任何帮助将不胜感激。非常感谢!
解决方案不要忘记,我有同样的问题,它是通过定义启用,导出为true在我的服务,试图做我喜欢的,..
< 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/>
UPDATE
Unfortunately, I didn't solve this issue and what I did is create a new project. Fortunately my new project works. One thing I noticed
from my newly created project, when I am sending notification
messages, some messages didn't arrive to my device. So I think its my
Internet connection problem (my idea only).
I am trying to implement basic receiving of Push Notifications using Firebase. I based this tutorial:
https://www.codementor.io/android/tutorial/send-push-notifications-to-android-with-firebase
My Problem is I don't receive any messages at all. I have sent more than 5 messages using the Firebase Console and still nothing happens.
Here is my implementation of 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
}
}
My FirebaseMessagingService implementation:
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());
}
}
My Manifest file:
<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>
What am I doing wrong? Or what am I lacking to implement?
I am really needing some help with this one. Any help will be greatly appreciated. Thank you very much!
解决方案 and dont forget, I had the same problem and it is solved by defining enabled, exported to true in my service, trying to do like me,..
<service android:name=".MyFirebaseMessagingService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</aplication>
and then set permission
<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" />
这篇关于android - Firebase通知推送通知不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!