2016-03-29 12 views
5

tôi đã thiết lập một thông báo cho ứng dụng của tôi và mã đi như thế này:Buttons Notification nhấp chuột không làm việc

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

ứng dụng hiển thị các thông báo nhưng không thực hiện bất kỳ hành động khi nút trên thông báo được nhấp xin vui lòng giúp đỡ tôi nếu một cái gì đó được thêm vào mã của tôi.

Cập nhật: Như Phương pháp này là bên trong một lớp mảnh

Tôi đã thay đổi một số mã của tôi như đề xuất dưới đây nhưng nó làm việc:

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

Thanks in advance ..

+0

bạn có thể muốn kiểm tra [this] (http://stackoverflow.com/questions/11270898/how-to-execute-a-method-by-clicking-a-notification). – zionpi

Trả lời

0

Bạn có thể cần phải thay thế 0 bằng PendingIntent.FLAG_UPDATE_CURRENT trong dòng này

PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), (int) System.currentTimeMillis(), intent, 0) ; 

Để biết thêm thông tin, hãy kiểm tra liên kết this.

0

điều tôi sử dụng trong ứng dụng của mình.

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, "Xem lời nhắc đầy đủ", pi) cũng hoạt động như nhấp vào thông báo. nó xuất hiện như một nút với văn bản đã đặt của bạn. vì vậy tôi không sử dụng, tôi thích bảo quản mã của mình.

0

Hãy thử mã sau có thể phù hợp với bạn.

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

Điều này đã không làm việc cho tôi .. – samridhgupta

+0

vấn đề là gì? –

+0

các nút thông báo không hoạt động khi nhấp, chúng không mở bất kỳ ý định nào hoặc các mục đích đang chờ xử lý .. – samridhgupta

Các vấn đề liên quan