서비스가 실행 중이고 알림을 보내려합니다. 너무 나쁘면 알림 개체에 Activity
과 같은 Context
이 필요하며 Service
이 아닙니다.Android에서 서비스에서 알림 보내기
전달 방법은 알고 계십니까? 각 알림에 대해 Activity
을 만들려고했으나 추한 것처럼 보였습니다. Activity
을 실행하지 않고도 View
을 실행할 수있는 방법을 찾을 수 없습니다.
서비스가 실행 중이고 알림을 보내려합니다. 너무 나쁘면 알림 개체에 Activity
과 같은 Context
이 필요하며 Service
이 아닙니다.Android에서 서비스에서 알림 보내기
전달 방법은 알고 계십니까? 각 알림에 대해 Activity
을 만들려고했으나 추한 것처럼 보였습니다. Activity
을 실행하지 않고도 View
을 실행할 수있는 방법을 찾을 수 없습니다.
모두 Activity
및 Service
실제로 extend
Context
그래서 당신은 단순히 당신 Service
내 Context
로 this
를 사용할 수 있습니다. 문서에서 본
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = /* your intent */;
notification.setLatestEventInfo(this, /* your content */, pendingIntent);
notificationManager.notify(/* id */, notification);
서비스에서 알림을 보내는 데 많은 문제가 있음을 명심하십시오. 문제가 발생하면 http://groups.google.com/group/android-developers/browse_thread/thread/e95740e776982f89 – Karussell
알림으로 업데이트하는 것이 좋습니다 .Builder API –
어떻게 업데이트 할 수 있습니까? Notification.Builder를 사용하여이 작업을 수행합니까? setLatestEventInfo는 이미 사용되지 않으므로 –
통지의 유형은 지원되지 않습니다 :
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "[email protected]")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
가장 좋은 방법은 :
@java.lang.Deprecated
public Notification(int icon, java.lang.CharSequence tickerText, long when) { /* compiled code */ }
public Notification(android.os.Parcel parcel) { /* compiled code */ }
@java.lang.Deprecated
public void setLatestEventInfo(android.content.Context context, java.lang.CharSequence contentTitle, java.lang.CharSequence contentText, android.app.PendingIntent contentIntent) { /* compiled code */ }
더 나은 방법을 당신은이 같은 통지를 보낼 수 있습니다
위 코드는 최소 API 레벨 11 (Android 3.0)이 필요합니다.
최소 API 레벨이 11보다 낮은 경우 support library의 NotificationCompat 클래스를 이와 같이 사용해야합니다. 최소 목표 API 레벨 인 경우
import android.support.v4.app.NotificationCompat;
-------------
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mylogo)
.setContentTitle("My Notification Title")
.setContentText("Something interesting happened");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(this, MyFavoriteActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
허용 된 것이 더 이상 사용되지 않기 때문에 이것이 가장 좋은 대답이되어야합니다. –
@MarcelKrivek 그 사람이 자신의 출처를 인용하는 것을 "잊었"것 같습니다. http://www.vogella.com/tutorials/AndroidNotifications/article.html – StarWind0
"NotificationReceiver"란 무엇입니까? – user3690202
그럼 내 솔루션이 최선의 방법 인 경우, 잘 모르겠어요. NotificationBuilder
내 코드를 사용하면 그 다음과 같습니다
private void showNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
매니페스트 :
<activity
android:name=".MainActivity"
android:launchMode="singleInstance"
</activity>
여기에 서비스 : 정말 Service
에서 singleTask
가있는 경우
<service
android:name=".services.ProtectionService"
android:launchMode="singleTask">
</service>
나도 몰라 하지만 내 응용 프로그램에서 제대로 작동 ...
여기에 빌더는 무엇입니까? –
그것은 NotificationCompat.Builder입니다 ... –
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void PushNotification()
{
NotificationManager nm = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0);
//set
builder.setContentIntent(contentIntent);
builder.setSmallIcon(R.drawable.cal_icon);
builder.setContentText("Contents");
builder.setContentTitle("title");
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
nm.notify((int)System.currentTimeMillis(),notification);
}
그냥 작동합니다. 감사! –
이러한 작업이 없으면 context
또는 this
대신 getBaseContext()
을 사용해보십시오.
이러한 시나리오는'getBaseContext()'를 사용하면 안됩니다. –
음 ... 서비스 _is_ 컨텍스트! –
하느님, 나는 그런 벙어리. 좋아, 모두의 시간을 낭비해서 미안해. –
괜찮습니다. 좋은 Google 질문입니다. –