在Firebase控制台设置后,Android计划未显示通知
我正在学习如何从firebase Console发送定期通知到我的Android应用程序:
homepage.java :
protected void onCreate(@Nullable Bundle savedInstanceState) {
startService(new Intent(getApplicationContext(),firebase_connection.class));
//Here I am calling the service class
}
firebase_connection.java :
public class firebase_connection extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("MyNotifications","MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
FirebaseMessaging.getInstance().subscribeToTopic("electric").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "Welcome to my app";
if(!task.isSuccessful())
msg = "Sorry";
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
}
});
}
}
我的firebase Console :
我一直在等待直到9:40 am(如我的通知设置中设置的设置给定上述屏幕截图),没有出现通知。我是Firebase的新手,我要去哪里出错?请帮我。
我正在实际设备上运行该应用
I am learning how to send periodic notification into my android app from my Firebase console:
Homepage.java:
protected void onCreate(@Nullable Bundle savedInstanceState) {
startService(new Intent(getApplicationContext(),firebase_connection.class));
//Here I am calling the service class
}
firebase_connection.java:
public class firebase_connection extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("MyNotifications","MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
FirebaseMessaging.getInstance().subscribeToTopic("electric").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "Welcome to my app";
if(!task.isSuccessful())
msg = "Sorry";
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
}
});
}
}
My Firebase console:
This is my scheduled notification
I kept waiting till 9:40 AM (as set in my notifications settings given int the above screenshot) and no notification showed up. I am new to Firebase, where am I going wrong? Please help me.
I am running the app on an actual device
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少很多东西。首先,您必须在
androidManifest
内部声明firebasemessagingservice
在application> application> application
tag:而不是调用 in in in
in
代码> on Createe
方法,您必须订阅主题。目前,您在通知服务本身中订阅,这是完全错误的。最后,在
firebasemessagingservice
中,您只是创建一个通知频道,而不是创建通知本身。这样做:您可以使用
remotemessage
对象来获取标题和您从firebase控制台发送的文本,如上所述。希望它能解决!There is a bunch of stuff you are missing. Firstly, you have to declare
FirebaseMessagingService
insideAndroidManifest
like this withinapplication
tag:And instead of calling
startService
inonCreate
method, you have to subscribe to topic. Right now, you are subscribing in the notification service itself which is completely wrong.Finally, in the
FirebaseMessagingService
, you are just creating a notification channel but not creating a notification itself. Do it like this:And you can use
RemoteMessage
object to fetch the title and the text you sent from the Firebase Console like above. Hope it works out!