【问题标题】:Notification not showing but code working fine通知未显示,但代码工作正常
【发布时间】:2018-11-20 06:01:49
【问题描述】:

我的代码没有错误,但通知不会出现在我的 Android 手机中(我曾尝试过 Android 8.0 和 9.0)。我希望在检测到来自 firebase 的值低于 50 时显示通知。我什至通过单击按钮调用通知来尝试最基本的代码但仍然无法正常工作..

请帮帮我,非常感谢

这是我的代码

public class IService extends Service {
    private final String CHANNEL_ID="personal_notifications";
    private final int NOTIFICATION_ID=001;

    public IService() {
    }

    NotificationManagerCompat notificationManager;
    DatabaseReference database=FirebaseDatabase.getInstance().getReference();

    public void onCreate(){
        super.onCreate();
        notificationManager=NotificationManagerCompat.from(this);

        database.child("Moisture").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot noteDataSnapshot: dataSnapshot.getChildren()){
                    Integer value=noteDataSnapshot.getValue(Integer.class);
                    if(value<50){
                        displayNotification();
                    }

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    public void displayNotification(){
        createNotificationChannel();
        Intent landingIntent= new Intent(this,MainGarden.class);
        landingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent landingPendingIntent= PendingIntent.getActivity(this,0,landingIntent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);
        builder.setSmallIcon(R.drawable.alarm);
        builder.setContentTitle("Alert! ");
        builder.setContentText(" There is a strong vibration detected on the Front Door");
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setContentIntent(landingPendingIntent);
        builder.setAutoCancel(true);

        NotificationManagerCompat notificationManagerCompat= NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());

    }
    private  void createNotificationChannel()
    {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        {
            CharSequence name= "Personal Notifications";
            String description = "Include all the personal notifications";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,name,importance);

            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

【问题讨论】:

    标签: android firebase notification-channel


    【解决方案1】:

    点击此链接获取正确答案。您是否添加了在 Firebase 控制台中创建的 google.json 文件。如果没有,请先创建 json 并将其添加到您的项目中。

    在 Android for Push 通知中,您可以使用 Firebase 通知。在 Android 应用中添加通知功能很简单。

    https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

    打开此链接并按照以下步骤实现推送通知。

    【讨论】:

    • 是的,连接工作正常。 Juz 通知不起作用
    猜你喜欢
    • 2022-11-02
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2017-11-16
    • 1970-01-01
    相关资源
    最近更新 更多