अधिसूचना का उपयोग कैसे करें


100

मैंने पाया कि मैं noficitations के लिए एक पदावनत विधि का उपयोग कर रहा हूं (सूचना ।setLatestEventInfo ())

यह Notification.Builder का उपयोग करने के लिए कहता है।

  • मैं इसे कैसे इस्तेमाल करूं?

जब मैं एक नया उदाहरण बनाने की कोशिश करता हूं, तो यह मुझे बताता है:

Notification.Builder cannot be resolved to a type

मैंने देखा कि यह एपीआई स्तर 11 (एंड्रॉइड 3.0) से काम करता है।
एलेक्स जुआन

जवाबों:


86

यह एपीआई 11 में है, इसलिए यदि आप 3.0 से पहले कुछ भी विकसित कर रहे हैं, तो आपको पुराने एपीआई का उपयोग जारी रखना चाहिए।

अद्यतन : NotificationCompat.Builder वर्ग को समर्थन पैकेज में जोड़ा गया है ताकि हम एपीआई स्तर v44 का समर्थन करने के लिए इसका उपयोग कर सकें:

http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html


धन्यवाद। मुझे आश्चर्य है कि यह उल्लेख नहीं करता है कि फंक्शन पेजों पर खुद
सारिको

15
हाँ: मेरी राय में अविकसित चेतावनी थोड़ी समयपूर्व है, लेकिन मुझे क्या पता है।
फेमी

152

अधिसूचना ।Builder API 11 या NotificationCompat.Builder API 1

यह एक उपयोग उदाहरण है।

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

13
मैं देख रहा हूँ कि v4 सपोर्ट पैकेज में ऐसा करने की एक तकनीक है: NotificationCompat.Builder
stanlick

6
मुझे लगता है कि किसी को Google को बताना चाहिए कि उनके पास Notification.Builderडॉक्स पृष्ठ में गंभीर टाइपोस है । मैं वही कर रहा था जो वे कह रहे थे लेकिन इसका कोई मतलब नहीं था। मैं यहां आता हूं और देखता हूं कि यह अलग है। मैं वास्तव में आपके उत्तर की सराहना करता हूं क्योंकि इसने मुझे डॉक्टर पर गलती से अवगत कराया।
एंडी

5
प्रलेखन कहता builder.getNotification()है कि पदावनत कर दिया गया है। यह कहता है कि आपको उपयोग करना चाहिए builder.build()
मिनेरी

26
NotificationBuilder.build () एपीआई स्तर 16 या उच्चतर की आवश्यकता है। एपीआई स्तर 11 और 15 के बीच कुछ भी आपको NotificationBuilder.getNotification () का उपयोग करना चाहिए।
केमिली स्वेंग

4
@MrTristan: जैसा कि प्रलेखन में लिखा गया है setSmallIcon(), setContentTitle()और setContentText()न्यूनतम आवश्यकताएं हैं।
कांव-कांव

70

चयनित उत्तर के अलावा यहां स्रोत ट्रिक्सNotificationCompat.Builder से वर्ग के लिए कुछ नमूना कोड है :

// Add app running notification  

    private void addNotification() {



    NotificationCompat.Builder builder =  
            new NotificationCompat.Builder(this)  
            .setSmallIcon(R.drawable.ic_launcher)  
            .setContentTitle("Notifications Example")  
            .setContentText("This is a test notification");  

    Intent notificationIntent = new Intent(this, MainActivity.class);  
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   
            PendingIntent.FLAG_UPDATE_CURRENT);  
    builder.setContentIntent(contentIntent);  

    // Add as notification  
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
    manager.notify(FM_NOTIFICATION_ID, builder.build());  
}  

// Remove notification  
private void removeNotification() {  
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
    manager.cancel(FM_NOTIFICATION_ID);  
}  

5
नए संगत बिल्डर का उपयोग करके पहला कोड जो वास्तव में काम करता है। बहुत बढ़िया!
जेम्स एमवी

1
मेरे लिए भी अच्छा काम किया। दो नोट: 1) आपको "ic_launcher" के लिए एक 32x32 आइकन बनाना होगा। पारदर्शी पृष्ठभूमि पर सफेद ड्राइंग 2) आपको int FM_NOTIFICATION_ID = [yourFavoriteRandom] के लिए कुछ यादृच्छिक संख्या को परिभाषित करने की आवश्यकता होगी;
एंडर्स 8

1
बहुत बहुत धन्यवाद, मेरी समस्या थी: जब मैंने दूसरी बार अधिसूचना पर क्लिक किया, तो पिछले टुकड़े खुले थे, और इस पंक्ति "PendingIntent.FLAG_UPDATE_CURRENT" ने मेरी समस्या को हल किया, और मेरा दिन बना दिया
श्रुति

4

अधिसूचना बिल्डर एंड्रॉइड एपीआई स्तर 11 और उससे ऊपर (एंड्रॉइड 3.0 और ऊपर) के लिए कड़ाई से है।

इसलिए, यदि आप हनीकॉम्ब टैबलेट को लक्षित नहीं कर रहे हैं, तो आपको अधिसूचना बिल्डर का उपयोग नहीं करना चाहिए, बल्कि निम्नलिखित उदाहरण की तरह पुराने अधिसूचना निर्माण विधियों का पालन करना चाहिए ।


4
आप संगतता लाइब्रेरी का उपयोग कर सकते हैं, इसलिए आप इसे एपीआई 4 या उच्चतर पर उपयोग कर सकते हैं।
लिएंड्रो

3

अद्यतन Android-N (मार्च -2016)

कृपया अधिक जानकारी के लिए अधिसूचना अपडेट लिंक पर जाएं ।

  • प्रत्यक्ष उत्तर
  • बंडल किए गए अधिसूचनाएं
  • कस्टम व्यूज़

Android N आपको एकल सूचना के रूप में प्रदर्शित होने के लिए समान सूचनाओं को बंडल करने की भी अनुमति देता है। इसे संभव बनाने के लिए, Android N मौजूदा NotificationCompat.Builder.setGroup()पद्धति का उपयोग करता है । उपयोगकर्ता सूचनाओं में से प्रत्येक का विस्तार कर सकते हैं, और अधिसूचना जैसे शेड से व्यक्तिगत रूप से प्रत्येक सूचना पर उत्तर और बर्खास्तगी जैसी कार्रवाई कर सकते हैं।

यह एक पूर्व-मौजूदा नमूना है जो एक साधारण सेवा दिखाता है जो अधिसूचना अधिसूचना का उपयोग करके सूचनाएं भेजता है। उपयोगकर्ता से प्रत्येक अपठित वार्तालाप को एक अलग अधिसूचना के रूप में भेजा जाता है।

यह नमूना Android N में उपलब्ध नई अधिसूचना सुविधाओं का लाभ लेने के लिए अद्यतन किया गया है।

नमूना कोड


नमस्ते, क्या आप बता सकते हैं कि यह विधि एंड्रॉइड 6.0 पर कैसे काम करती है, जब हम डाउनलोडर_लिफ्ट्स का उपयोग कर रहे हैं। मैं ग्रहण एसडीके पर हूँ - २५.१.se || ADT 23.0.X दुख की बात है || Google
एपीके

2

मुझे एक समस्या निर्माण सूचनाएँ मिल रही थीं (केवल Android 4.0+ के लिए विकसित)। इस लिंक ने मुझे वही दिखाया जो मैं गलत कर रहा था और निम्नलिखित कहता है:

Required notification contents

A Notification object must contain the following:

A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()

मूल रूप से मैं इनमें से एक को याद कर रहा था। बस इसके साथ समस्या निवारण के लिए एक आधार के रूप में, सुनिश्चित करें कि आपके पास इनमें से बहुत कम से कम है। उम्मीद है कि यह किसी और को सिरदर्द से बचाएगा।


इसलिए यदि आप सोचते हैं: "मुझे बाद में एक आइकन मिलेगा", तो आपको कोई सूचना-प्रेम नहीं मिलेगा। इस एक के लिए धन्यवाद;)
ननन

1

मामले में यह किसी को भी मदद करता है ... मुझे नए पुराने पुराने एपीआई के खिलाफ परीक्षण करते समय समर्थन पैकेज का उपयोग करके सूचनाएं स्थापित करने में बहुत परेशानी हो रही थी। मैं उन्हें नए डिवाइस पर काम करने में सक्षम कर रहा था लेकिन पुराने डिवाइस पर एक त्रुटि परीक्षण मिलेगा। मेरे लिए काम करना आखिरकार क्या था, अधिसूचना कार्यों से संबंधित सभी आयातों को हटाना। विशेष रूप से NotificationCompat और TaskStackBuilder। ऐसा लगता है कि शुरुआत में मेरा कोड सेट करते समय आयात जहां नए बिल्ड से जोड़ा गया है और समर्थन पैकेज से नहीं। फिर जब मैं इन वस्तुओं को बाद में ग्रहण में लागू करना चाहता था, तो मुझे उन्हें फिर से आयात करने के लिए प्रेरित नहीं किया गया। आशा है कि समझ में आता है, और यह किसी और की मदद करता है :)


1

यह एपीआई 8 में भी काम करता है आप इस कोड का उपयोग कर सकते हैं:

 Notification n = 
   new Notification(R.drawable.yourownpicturehere, getString(R.string.noticeMe), 
System.currentTimeMillis());

PendingIntent i=PendingIntent.getActivity(this, 0,
             new Intent(this, NotifyActivity.class),
                               0);
n.setLatestEventInfo(getApplicationContext(), getString(R.string.title), getString(R.string.message), i);
n.number=++count;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.flags |= Notification.DEFAULT_SOUND;
n.flags |= Notification.DEFAULT_VIBRATE;
n.ledARGB = 0xff0000ff;
n.flags |= Notification.FLAG_SHOW_LIGHTS;

// Now invoke the Notification Service
String notifService = Context.NOTIFICATION_SERVICE;
NotificationManager mgr = 
   (NotificationManager) getSystemService(notifService);
mgr.notify(NOTIFICATION_ID, n);

या मैं इस बारे में एक उत्कृष्ट ट्यूटोरियल का पालन करने का सुझाव देता हूं


1

मैंने उपयोग कर लिया है

Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());

0
          // This is a working Notification
       private static final int NotificID=01;
   b= (Button) findViewById(R.id.btn);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Notification notification=new       Notification.Builder(MainActivity.this)
                    .setContentTitle("Notification Title")
                    .setContentText("Notification Description")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notification.flags |=Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(NotificID,notification);


        }
    });
}

0

स्वयं निहित उदाहरण

इस उत्तर में समान तकनीक लेकिन:

  • आत्म निहित: कॉपी पेस्ट और यह संकलन और चलाएगा
  • आप के लिए एक बटन के साथ के रूप में आप के रूप में कई सूचनाएं उत्पन्न करने के लिए और इरादे और अधिसूचना आईडी के साथ खेलते हैं

स्रोत:

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
    private int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Button button = new Button(this);
        button.setText("click me");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Notification notification = new Notification.Builder(Main.this)
                        /* Make app open when you click on the notification. */
                        .setContentIntent(PendingIntent.getActivity(
                                Main.this,
                                Main.this.i,
                                new Intent(Main.this, Main.class),
                                PendingIntent.FLAG_CANCEL_CURRENT))
                        .setContentTitle("title")
                        .setAutoCancel(true)
                        .setContentText(String.format("id = %d", Main.this.i))
                        // Starting on Android 5, only the alpha channel of the image matters.
                        // https://stackoverflow.com/a/35278871/895245
                        // `android.R.drawable` resources all seem suitable.
                        .setSmallIcon(android.R.drawable.star_on)
                        // Color of the background on which the alpha image wil drawn white.
                        .setColor(Color.RED)
                        .build();
                final NotificationManager notificationManager =
                        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(Main.this.i, notification);
                // If the same ID were used twice, the second notification would replace the first one. 
                //notificationManager.notify(0, notification);
                Main.this.i++;
            }
        });
        this.setContentView(button);
    }
}

Android 22 में परीक्षण किया गया।

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