15

पर क्लिक करें यह कोड एक अधिसूचना बनाता है। आप इसे क्लिक करते हैं, वर्तमान आवेदन भाग गया है (आशय Entry में बनाई गई है, जो अपने ही Activity), एक Android डेवलपर ब्लॉग का एक थोड़ा संशोधित संस्करण:अधिसूचना पर एंड्रॉइड कॉल विधि

private void makeIntent() { 
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification note = new Notification(R.drawable.prev, "Status message!", System.currentTimeMillis()); 
    Intent intent = new Intent(this, Entry.class); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
    note.setLatestEventInfo(this, "New Email", "Unread Conversation", pi); 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 
    mgr.notify(NOTIFY_ME_ID, note); 
} 

लेकिन मैं शुरू करने के लिए नहीं करना चाहते हैं कोई गतिविधि, लेकिन केवल वर्तमान गतिविधि में एक विधि चलाने के लिए। मैंने जो अभी तक पढ़ा है, मुझे लगता है कि मुझे startActivityForResult() जैसे विधियों का उपयोग करना है, intent-filters का उपयोग करें और onActivityResult() लागू करें, लेकिन उन सभी चीजों के साथ गड़बड़ करने के बाद, Intent और PendingIntent में चीजों को बदलना, मेरे पास अभी भी कोई उपयोगी परिणाम नहीं है। क्या किसी भी तरह से Entry (मेरा मुख्य Activity, जिसमें Intent बनाया गया है) में किसी विधि को कॉल करना संभव है, या जब मैं अपने नव निर्मित Notification पर क्लिक करता हूं तो किसी भी आउटगोइंग या आने वाले Intents को पकड़ सकता है?

पीएस। मेरी माफी माँगती है अगर यह एक डुप्लिकेट थ्रेड है, तो अभी बहुत धीमी है, मैं ठीक से खोज नहीं कर सकता।

उत्तर

13

अपने मैनिफ़ेस्ट फ़ाइल में अपने activity में android:launchMode="singleTop" जोड़ें, विधि protected void onNewIntent(Intent intent) { ... } है और उपयोग करने के लिए इस कोड: एक विधि में

प्लेस इस कोड:

private static final int MY_NOTIFICATION_ID = 1; 
private NotificationManager notificationManager; 
private Notification myNotification; 

void notification() { 
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    myNotification = new Notification(R.drawable.next, "Notification!", System.currentTimeMillis()); 
    Context context = getApplicationContext(); 
    String notificationTitle = "Exercise of Notification!"; 
    String notificationText = "http://android-er.blogspot.com/"; 
    Intent myIntent = new Intent(this, YourActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION); 
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 
    myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent); 
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification); 
} 
+3

काम करता है, धन्यवाद। – stealthjong

+1

बेस्ट समाधान Google ने मुझे दिया, धन्यवाद। –

+0

जैसा कि बताया गया है एंड्रॉइड जोड़ने के लिए मत भूलना: launchMode = "singleTop" –

0
Intent intent = new Intent(this, Notificationintent.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 



    Notification noti = new Notification.Builder(this) 
    .setContentTitle("APP NOTIFICATION") 
    .setContentText(messageValue) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setStyle(new Notification.BigTextStyle() 
    .bigText(messageValue)) 
    .setContentIntent(pIntent).build(); 

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
// hide the notification after its selected 
noti.flags |= Notification.FLAG_AUTO_CANCEL; 

notificationManager.notify(0, noti); 
+0

यहां अधिसूचना एक और गतिविधि है, और संदेशवृद्धि एक स्ट्रिंग है। –

4

यह मेरे लिए 100% काम किया :

Intent intent = new Intent(this, YourClass.class); 
    intent.putExtra("NotiClick",true); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     Notification Noti; 
     Noti = new Notification.Builder(this) 
       .setContentTitle("YourTitle") 
       .setContentText("YourDescription") 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentIntent(pIntent) 
       .setAutoCancel(true).build(); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     notificationManager.notify(0, Noti); 
    } 

फिर ऑनक्रेट/सह में अपनी कक्षा के nstructor यह करते हैं:

if (savedInstanceState == null) { 
     Bundle extras = getIntent().getExtras(); 
     if(extras == null) 
     { 
      //Cry about not being clicked on 
     } 
     else if (extras.getBoolean("NotiClick")) 
     { 
      //Do your stuff here mate :) 
     } 

    } 
संबंधित मुद्दे