كيفية استيراد NotificationCompat.Builder في Android Studio
To resolve the error message you’re encountering, you need to make sure that the NotificationCompat.Builder
class you’re using matches the rest of your imports. Since you’re using android.support.v7.app.NotificationCompat
for other imports, you should also use android.support.v7.app.NotificationCompat.Builder
for your mBuilder
declaration.
Here’s how you can update your code:
-
دور وضرورة كلمة finally في برمجة Java03/02/2024
-
Enum vs Union Types in TypeScript14/04/2024
-
تحويل مصفوفة كائنات باستخدام map ES615/04/2024
javaimport android.support.v7.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final int NOTIFICATION_ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button notificationButton = (Button) findViewById(R.id.notification_button);
notificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("My Notification")
.setContentText("Hello, this is a notification!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
});
}
}
In this code snippet, replace NotificationActivity.class
with the name of the activity you want to launch when the notification is clicked, and replace CHANNEL_ID
with the ID of the notification channel you’ve defined in your app.
This should resolve the error and allow you to use android.support.v7.app.NotificationCompat.Builder
in your app.
المزيد من المعلومات
لحل المشكلة التي تواجهها، يجب عليك التأكد من توافق إصدارات مكتبات الدعم التي تستخدمها في مشروعك. إذا كنت تستخدم android.support.v7.app.NotificationCompat
في باقي الاستيرادات، يجب عليك استخدام نفس الإصدار لـ NotificationCompat.Builder
.
تحقق من ملف build.gradle
الخاص بمشروعك وتأكد من استخدام نفس إصدارات مكتبات الدعم. على سبيل المثال، إذا كنت تستخدم مكتبة الدعم بإصدار 27.1.1
، يجب أن تضيف التالي في ملف build.gradle
:
gradledependencies { implementation 'com.android.support:appcompat-v7:27.1.1' }
ثم قم بتحديث الاستيراد في ملف الكود ليكون كالتالي:
javaimport android.support.v7.app.NotificationCompat;
بعد ذلك، قم ببناء المشروع مرة أخرى وجرب تشغيل التطبيق. يجب أن تختفي رسالة الخطأ وتعمل الإشعارات بشكل صحيح.