简体   繁体   English

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

[英]Android scheduled notification not showing, after setting it up in Firebase console

I am learning how to send periodic notification into my android app from my Firebase console:我正在学习如何从我的 Firebase 控制台向我的 android 应用程序发送定期通知:

Homepage.java :主页.java

protected void onCreate(@Nullable Bundle savedInstanceState) {            

  startService(new Intent(getApplicationContext(),firebase_connection.class)); 
 //Here I am calling the service class
        
}

firebase_connection.java : 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 :我的 Firebase 控制台

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.我一直等到上午 9 点 40 分(如上面屏幕截图中我的通知设置中所设置的那样),但没有出现任何通知。 I am new to Firebase, where am I going wrong?我是 Firebase 的新手,我哪里出错了? Please help me.请帮我。

I am running the app on an actual device我在实际设备上运行应用程序

There is a bunch of stuff you are missing.您缺少很多东西。 Firstly, you have to declare FirebaseMessagingService inside AndroidManifest like this within application tag:首先,您必须在AndroidManifest中声明FirebaseMessagingService ,如下所示在application标签中:

<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.而不是在onCreate方法中调用startService ,您必须订阅主题。 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.最后,在FirebaseMessagingService中,您只是创建了一个通知通道,而不是创建通知本身。 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.您可以使用RemoteMessage对象来获取您从 Firebase 控制台发送的标题和文本,如上所示。 Hope it works out!希望能成功!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM