Android में एक सेवा से एक अधिसूचना भेजना


105

मेरे पास एक सेवा चल रही है, और मैं एक अधिसूचना भेजना चाहूंगा। बहुत बुरा, अधिसूचना वस्तु एक की आवश्यकता है Context, एक तरह Activityऔर, नहीं एक Service

क्या आप पास से गुजरने का कोई तरीका जानते हैं? मैंने Activityप्रत्येक अधिसूचना के लिए बनाने की कोशिश की , लेकिन यह बदसूरत लगता है, और मुझे Activityबिना किसी लॉन्च के कोई रास्ता नहीं मिल सकता है View


14
उम्म ... एक सेवा है एक संदर्भ!
इसहाक वालर 19

19
भगवान, मैं ऐसी डंबबस हूं। ठीक है, सबका समय बर्बाद करने के लिए क्षमा करें।
ई-सिटिस

28
यह ठीक है - यह एक अच्छा Google प्रश्न है।
आइजैक वालर

अपनी दूसरी टिप्पणी की तरह: D: D
Faizan Mubasher

इस पोस्ट ने सिर्फ मेरा दिन बचाया ...
मुहम्मद फैजान

जवाबों:


109

दोनों Activityऔर Serviceवास्तव में extend Contextतो आप बस thisअपने Contextभीतर का उपयोग कर सकते हैं Service

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);

4
ध्यान रखें कि आपको किसी सेवा से अधिसूचना करने में बहुत समस्याएँ होंगी। यदि आपको समस्या है, तो इस समूह पर एक नज़र डालें। http://www.group/android-developers/browse_thread/thread/…
Karussell

1
आप अधिसूचना का उपयोग करके यह कैसे कर सकते हैं। क्योंकि setLatestEventInfo पहले से ही पदावनत है।
काइरी सैन

77

इस प्रकार की अधिसूचना को दस्तावेजों से देखा गया है:

@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 */ }

बेहतर तरीका
आप इस तरह से एक अधिसूचना भेज सकते हैं:

// 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 " + "test@gmail.com")
        .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); 

सबसे अच्छा तरीका
कोड ऊपर न्यूनतम एपीआई स्तर 11 (Android 3.0) की जरूरत है।
यदि आपका न्यूनतम एपीआई स्तर 11 से कम है, तो आपको इस तरह से समर्थन पुस्तकालय के नोटिफिकेशन.कॉम क्लास का उपयोग करना चाहिए ।

इसलिए यदि आपका न्यूनतम लक्ष्य एपीआई स्तर 4+ है (Android 1.6+) इसका उपयोग करें:

    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());

4
यह सबसे अच्छा जवाब होना चाहिए क्योंकि स्वीकृत एक को हटा दिया गया है
मार्सेल क्रिकक

4
@MarcelKrivek लगता है कि वह या वह अपने स्रोत को उद्धृत करने के लिए "भूल गया"। vogella.com/tutorials/AndroidNotifications/article.html
StarWind0

"NotificationReceiver" क्या है?
user3690202

"NotificationReceiver" वह गतिविधि है जिसे अधिसूचना द्वारा खोला जाएगा। @ StarWind0 द्वारा दिए गए लिंक की जाँच करें।
जॉर्ज थियोडोरकिस

अधिसूचना। इसका अब कोई सबसे अच्छा जवाब नहीं है
शैतान का ड्रीम

7
@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);
}

विषय का प्रश्न एक सेवा से नहीं गतिविधि से है
ड्यूना

1

ठीक है, मुझे यकीन नहीं है कि मेरा समाधान सबसे अच्छा अभ्यास है। 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
        android:name=".services.ProtectionService"
        android:launchMode="singleTask">
    </service>

मुझे नहीं पता कि क्या वास्तव में एक है singleTask, Serviceलेकिन यह मेरे आवेदन पर ठीक से काम करता है ...


इसमें बिल्डर क्या है?
विश्वनाथ लीक्षमान

-2

यदि इन में से कोई भी काम, कोशिश getBaseContext()करने की बजाय contextया this


2
आपको getBaseContext()इन परिदृश्यों का उपयोग नहीं करना चाहिए ।
रहमत
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.