在Firebase控制台设置后,Android计划未显示通知

发布于 2025-02-04 10:18:30 字数 1707 浏览 4 评论 0原文

我正在学习如何从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2025-02-11 10:18:31

您缺少很多东西。首先,您必须在androidManifest内部声明firebasemessagingserviceapplication> application> application tag:

<service android:name=".firebase_connection">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

而不是调用 in in in in 代码> on Createe方法,您必须订阅主题。目前,您在通知服务本身中订阅,这是完全错误的。

protected void onCreate(@Nullable Bundle savedInstanceState) {            
  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();
       }
   });
}

最后,在firebasemessagingservice中,您只是创建一个通知频道,而不是创建通知本身。这样做:

public class firebase_connection extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {
        super.onMessageReceived(message);

        NotificationManager manager = getSystemService(NotificationManager.class);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("channel1","My Notification", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("description");
            manager.createNotificationChannel(channel);
        }
        
        if (remoteMessage.getNotification() != null) {
            Notification notification = NotificationCompat.Builder(context, "channel1")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(message.getNotification().getTitle())
            .setContentText(message.getNotification().getBody())
            .build();

             manager.notify(1, notification)
        }
    }
}

您可以使用remotemessage对象来获取标题和您从firebase控制台发送的文本,如上所述。希望它能解决!

There is a bunch of stuff you are missing. Firstly, you have to declare FirebaseMessagingService inside AndroidManifest like this within application tag:

<service android:name=".firebase_connection">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

And instead of calling startService in onCreate method, you have to subscribe to topic. Right now, you are subscribing in the notification service itself which is completely wrong.

protected void onCreate(@Nullable Bundle savedInstanceState) {            
  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();
       }
   });
}

Finally, in the FirebaseMessagingService, you are just creating a notification channel but not creating a notification itself. Do it like this:

public class firebase_connection extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {
        super.onMessageReceived(message);

        NotificationManager manager = getSystemService(NotificationManager.class);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("channel1","My Notification", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("description");
            manager.createNotificationChannel(channel);
        }
        
        if (remoteMessage.getNotification() != null) {
            Notification notification = NotificationCompat.Builder(context, "channel1")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(message.getNotification().getTitle())
            .setContentText(message.getNotification().getBody())
            .build();

             manager.notify(1, notification)
        }
    }
}

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文