0

我的代码没有错误,但通知不会出现在我的 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");
    }
}
4

1 回答 1

0

按照此链接获取正确答案。您是否添加了在 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/

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

于 2018-11-20T06:11:06.970 回答