【问题标题】:actions of notifications in androidandroid中通知的操作
【发布时间】:2014-11-10 19:10:02
【问题描述】:

首先真的很抱歉,如果这是重复的,虽然我认为这不是因为我搜索了很多但没有发现任何工作。我想删除单击取消的通知-我在某个地方找到了调用 .cancel() 方法的地方,但是我应该在哪里进行调用。当我单击接受或取消时,另一个意图值仅相同,即 logcat 中的“通知”。请让我知道我做错了什么。提前致谢。

我的通知创建者 -

Intent intent1 = new Intent(context, code.omnitrix.slidingmenu.MainActivity.class);
        if(message.contains("Blood Request")){
            intent1.putExtra("fragment", "Notifications");
            Log.d("Intent sent", intent1.getExtras().getString("fragment"));
        }else if(message.contains("Thank")){
            intent1.putExtra("fragment", "myDonations");
        }

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent1, PendingIntent.FLAG_CANCEL_CURRENT);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.ic_launcher);        
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        mBuilder.setContentText(message);
        //.setContentTitle("Thank you")
        if(message.contains("Blood Request")){
            mBuilder.setContentTitle("Blood Request");
            Intent intent2 = new Intent(context, code.omnitrix.slidingmenu.MainActivity.class);
            intent2.putExtra("fragment", "accept");
            PendingIntent acceptIntent = PendingIntent.getActivity(context, 0, intent2, 0);
            Intent intent3 = new Intent(context, GcmBroadcastReceiver.class);
            intent3.putExtra("fragment", "reject-1");
            PendingIntent rejectIntent = PendingIntent.getActivity(context, 0, intent3, 0);
            mBuilder.addAction(R.drawable.tick_32, "Accept", acceptIntent);
            mBuilder.addAction(R.drawable.cross_32, "Reject", rejectIntent);
        }


        mBuilder.setContentIntent(contentIntent);   
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(1, mBuilder.build());

虽然另一个文件是意图接收者,但它就像 -

Intent intent = getIntent();
            String fragmentToDisplay = intent.getStringExtra("fragment");
            Log.d("Vinit", "Intent Received");
            if(fragmentToDisplay!=null){
                Log.d("From Intent", fragmentToDisplay);
                if(fragmentToDisplay.equalsIgnoreCase("updateDetails")){
                    //show update details fragment
                    Fragment fragment = new UpdateDetails();
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit();      
                }else if(fragmentToDisplay.equalsIgnoreCase("Notifications")){
                    Fragment fragment = new Notifications();
                    FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit();
                }else if(fragmentToDisplay.equalsIgnoreCase("accept")){
                    //just send the response to the server.
                    //put in sqlite database.
                    Log.d("Fragment inside", "accept");
                }else if(fragmentToDisplay.equalsIgnoreCase("reject")){
                    //just remove the request from the notifications.
                    Log.d("Fragment inside", "reject");
                }
            }

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    1) 删除取消点击通知: 将此代码添加到您的接收器中

     if(fragmentToDisplay.equalsIgnoreCase("reject")){
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notificationId);
        Log.d("Fragment inside", "reject");
     }
    

    PS:您在代码 (mNotificationManager.notify(1, mBuilder.build());) 中使用了 "1" 作为 notificationId,最好的方法是通过 putExtra() 发送并在你的接收器。

    2) 抱歉,我不明白您问题的第二部分。你能解释更多吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 2020-01-30
      相关资源
      最近更新 更多