2016-03-29 12 views
5

मैं अपने अनुप्रयोग के लिए सेटअप एक सूचना मिली है और कोड इस प्रकार है:अधिसूचना बटन काम नहीं कर रहा क्लिक

public int getNotification(View view) { 

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

     Intent intent = new Intent(getActivity() ,RouteMap.class); 
     intent.putExtra("Stop Ride",true); 
     PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), (int) System.currentTimeMillis(), intent, 0) ; 

     if (Build.VERSION.SDK_INT < 16) { 
      getToast("API below 16 ...notification not supported"); 

     } else { 

      Notification notify = new Notification.Builder(getActivity()) 
        .setSmallIcon(R.drawable.icon1) 
        .setContentTitle("Car Rental") 
        .setContentText("Click to view ") 
        .setOngoing(true) 
        .setContentIntent(pendingIntent) 
        .addAction(R.drawable.icon3,"View ",pendingIntent) 
        .addAction(R.drawable.alert_icon,"Stop Ride", pendingIntent) 
        .build(); 

      notificationManager.notify(notificationID, notify); 

     } 

     return notificationID; 
    } 

    public void removeNotification() { 
     NotificationManager manager = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.cancel(
       notificationID); 
     getToast("Notification Removed"); 
    } 

एप्लिकेशन अधिसूचना दिखाने लेकिन किसी भी क्रिया को निष्पादित नहीं करता है जब अधिसूचना पर बटन क्लिक किया कृपया मदद अगर मेरे कोड में कुछ जोड़ा जाना है तो मुझे।

अद्यतन: नीचे सुझाव के रूप में के रूप में इस पद्धति का एक टुकड़ा वर्ग

मैं अपने कोड के कुछ बदल गया है के अंदर है, लेकिन यह काम नहीं किया:

public int getNotification(View view) { 

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

    Intent intent = new Intent(getActivity(), RouteMap.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0); 
    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); 
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    if (Build.VERSION.SDK_INT < 16) { 
     getToast("API below 16 ...notification not supported"); 

    } else { 

     Notification notify = new Notification.Builder(getActivity()) 
       .setSmallIcon(R.drawable.icon12) 
       .setContentTitle("Car Rental") 
       .setContentText("Click to view ") 
       .setOngoing(true) 
       .setSound(soundUri) 
       .setContentIntent(pendingIntent) 
       .addAction(R.drawable.icon3, "View ", pendingIntent) 
       .addAction(R.drawable.alert_icon, "Stop Ride", pendingIntent) 
       .build(); 

     notificationManager.notify(notificationID, notify); 

    } 

    return notificationID; 
} 

अग्रिम धन्यवाद ..

+0

आप देखना चाहें [इस] (http://stackoverflow.com/questions/11270898/how-to-execute-a-method-by-clicking-a-notification) बाहर। – zionpi

उत्तर

0

आपको 0PendingIntent.FLAG_UPDATE_CURRENT के साथ इस लाइन

में बदलने की आवश्यकता है
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), (int) System.currentTimeMillis(), intent, 0) ; 

अधिक जानकारी के लिए this लिंक देखें।

0

जो मैं अपने ऐप में उपयोग करता हूं।

Intent i = new Intent(ctx, NotifReceiver.class); 

     PendingIntent pi = PendingIntent.getActivity(ctx, notifNum, i, 0); 

     Notification notif = new Notification.Builder(ctx) 
       .setContentTitle("MyPower Reminder") 

       .setContentText(contentTxt) 
       .setSmallIcon(R.drawable.ic_mypower4) 
       .setContentIntent(pi) 
       .setSound(notifSound) 

       //.addAction(0, "View Full Reminder", pi) 
       .build(); 

     NotificationManager notifMgr = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE); 
     notifMgr.notify(0, notif); 

.addAction (0, "देखें पूर्ण रिमाइंडर", अनुकरणीय) भी अधिसूचना पर क्लिक तरह काम करता है। यह आपके सेट टेक्स्ट के साथ एक बटन की तरह दिखाई देता है। इसलिए मैं उपयोग नहीं करता, मुझे अपने कोड को संरक्षित करना पसंद है।

0

निम्नलिखित कोड का प्रयास करें यह आपके लिए काम हो सकता है।

Intent intent = new Intent(this, MainActivity.class); 
      PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 
      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext()) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle("App Name") 
        .setContentText(message) 
        .setContentIntent(pIntent) 
        .setSound(soundUri); //This sets the sound to play 
      notificationManager.notify(0, mBuilder.build()); 
+0

यह मेरे लिए काम नहीं किया .. – samridhgupta

+0

समस्या क्या थी? –

+0

अधिसूचना बटन क्लिक करते समय काम नहीं कर रहे हैं, वे किसी भी इरादे या लंबित इरादे नहीं खोलते हैं .. – samridhgupta

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