آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
مشکل عدم دسترسی خریداران پیشین به برخی آموزش ها برطرف شد
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

نمایش نتیفیکیشن

سینا  5 سال پیش  5 سال پیش
0 0

سلام. دوستان من بارها کد های مختلف رو نوشتم و از اینترنت پیدا کردم برای نمایش یک نتیفیکیشن ساده ولی هیچکدوم کار نکرد.

این کد زیر رو دیگه از خود داکیومنت های گوگل برداشتم کپی کردم ولی باز هم برنامه کرش میکنه و هیییچ پیغام خطا یا اروری در لاگ کت نشون نمیده که مشگل رو بفهمم. مشکل کد چیه؟

private void addNotification() {

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.sendsmsicon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, mBuilder.build());
}
+1 0
لینک لینک (5 سال پیش)
0 0
ممنونواز هر کدی میشد استفاده کردم ولی نشد. یکی بهم گفت ممکنه کد های کلی برنامه رو بد نوشته باشم و حافظه لیک اشغال میشه و نتیفیکیشن هم چون پردازش سیستمی داره به خاطر این کرش میکنه، خلاصه الان یه 10 روزی هست گیر اینم (5 سال پیش)
+2 0
لینک (5 سال پیش)
 برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال 
116236  5 سال پیش
+1 0

سلام

بسته به نسخه اندرویدی که دارین از یکی از کدهای زیر استفاده کنید:

برای API کمتر از 15:

  NotificationManager   myNotificationManager;

        public void lunchNotification() {
            myNotificationManager = (NotificationManager) getSystemService(G.context.NOTIFICATION_SERVICE);
            long when = System.currentTimeMillis();
            CharSequence tickerText = "Notification";
            CharSequence contentTitle = "در حال اجرا ...";
            CharSequence contentText = "نرم افزار در حال ارسال اطلاعات است";
            Intent intent = new Intent();
            PendingIntent contentIntent = PendingIntent.getActivity(G.context, 0, intent, 0);
            Notification notification = new Notification(R.drawable.icon, tickerText, when);
            notification.setLatestEventInfo(G.context, contentTitle, contentText, contentIntent);
            myNotificationManager.notify(1, notification);
}

برای API 15 و بالاتر از آن:

             NotificationManager mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
                ActivityLogs.this)
                .setSmallIcon(R.drawable.notification_small_icon)
                .setContentTitle("Your title")
                .setContentText("Your message")
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon));
            Intent resultIntent = new Intent(ActivityLogs.this, ActivityLogs.class);
            PendingIntent resultPendingIntent = PendingIntent.getActivity(ActivityLogs.this, 0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            Notification notification = mBuilder.build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL; // برای محو شدن ناتیفیکیشن بعد از یک بار لمس
            mNotifyManager.notify(1, notification);
0 0
ممنون بابت وقتی که برای قرار دادن کد گذاشتید. این کد رو هم تست کردم و باز همون ارور رو داد، فقط رو گوشی با رم 4 کار کرد (5 سال پیش)

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .