ورود

View Full Version : نمایش ناتیفیکیشن وقتی که برنامه بسته است !؟



ghasem110deh
چهارشنبه 22 شهریور 1396, 19:47 عصر
سلام به همه :-)
دوستان چطور وقتی برنامه بسته است ، ناتیفیکیشن بفرستم ؟

توی برنامه یه سری دیتا بر اساس تاریخ ذخیره شده، حالا میخوام توی سررسید اون تاریخ ناتیفیکیشن نمایش داده بشه (حتی اگه برنامه بسته باشه)
بعد هم که با کلیک روی ناتیفیکیشن برنامه باز بشه (که این قسمت حل شده)

ghasem110deh
پنج شنبه 23 شهریور 1396, 15:12 عصر
این خطا رو چطوری رفع و رجوع کنم ؟ مشکل چیه !؟



Process: ir.rahgoshafan.mahanmember, PID: 4049
java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.rahgoshafan.mahanmember/ir.rahgoshafan.mahanmember.MainActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=ir.rahgoshafan.mahanmember.NotiService }


اینم کد : به این خط گیر میده
this.startService(serviceIntent);
package ir.rahgoshafan.mahanmember;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class NotiService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

package ir.rahgoshafan.mahanmember;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("ir.rahgoshafan.mahanmember.NotiService");
context.startService(serviceIntent);
}
}
}

اینم توی اکتیویتی که قراره ناتیفیکیشن رو ارسال کنه :
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (NotiService.class.getName().equals(service.servic e.getClassName())) {
return true;
}
}
return false;
}

اینم توی onCreate

if (!isMyServiceRunning()) {
Intent serviceIntent = new Intent("ir.rahgoshafan.mahanmember.NotiService");
this.startService(serviceIntent);
}

#root#
پنج شنبه 23 شهریور 1396, 20:13 عصر
Intent شما برای اجرای سرویس باید صریح باشه یعنی این شکلی:

new Intent(context, NotiService.class)

ghasem110deh
یک شنبه 26 شهریور 1396, 19:21 عصر
دوستان اون قبلی جواب نداد : اما این کد پایین پیغام رو میفرسته ولی باید برنامه باز باشه !!!


public class MyAlarmService extends Service {

@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
}

@SuppressWarnings({"static-access", "deprecation"})
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Notification notification = new Notification.Builder(this).setContentTitle("سالگرد خاطره")
.setContentText("امروز سالگرد" + " 1 " + "خاطره ثبت شده می باشد").setSmallIcon(R.mipmap.ic_launcher).setContentInt ent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}

@Override
public void onDestroy() {
super.onDestroy();
}
}


public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}


و اینم توی اکتیویتی اصلی :

if (remember > 0) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 30);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

#root#
یک شنبه 26 شهریور 1396, 22:05 عصر
من زیاد سروکله نزدم با notif، شاید دوستان تجربه بیشتری داشته باشن، ولی اینو جستجو کنید تو جوابایی که دادن یه سری نکات میشه پیدا کرد:

android service persistent notification

بعدم سعی کنید جایگزین اون کدهای منسوخ شده رو پیدا کنید و از اونا استفاده کنید. الان به راهنمای notif توی مستندات نگاه میکنم خیلی جامع توضیح داده شده.

ghasem110deh
سه شنبه 28 شهریور 1396, 15:19 عصر
الان مشکل ارسال هم تقریبا حل شد ... منتها یه شرط گذاشتم "if (remember > 0) {

"

که اگه بیشتر از صفر بود پیام ارسال بشه وو ولی اگه صفر هم باشه میفرسته ! شرط مشکل داره !؟ یا چون آلرت هست زیاد شرط رو جدی نمیگیره :-)