【问题标题】:Notification service not working after clicking notification message in status bar单击状态栏中的通知消息后通知服务不起作用
【发布时间】:2012-04-25 06:28:19
【问题描述】:

我参考了以下链接来研究 Android 通知服务的演示示例:Sai Geetha BlogVogella Tutorial

两者都有效但部分有效,即我已经按原样下载了两个项目并执行了它们。两者都有按钮来启动通知。 On Button Click 通知出现在顶部状态栏。

问题来了,点击该通知后,既没有显示任何消息,也没有触发意图导航到新活动。

我是这个概念的新手,所以任何帮助表示赞赏......

编辑

CreateNotification .class

public class CreateNotification extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void createNotification(View view) {
        NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final int UNIQUE_ID = 123458;
        Intent navigationIntent = new Intent();
        navigationIntent.setClass(CreateNotification.this,
                NotificationReceiver.class);

        PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
                0);
        String body = "New Notification added!!!";
        String title = "Title";
        Notification n = new Notification(R.drawable.ic_launcher, body,
                System.currentTimeMillis());
        n.number = 2;
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_ALL;
        nm.notify(UNIQUE_ID, n);
    }
}

NotificationReceiver.class

public class NotificationReceiver extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        Log.i("Receiver", "NotificationReceiver");
    }
}

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="createNotification"
        android:text="Create Notification" >
    </Button>

</LinearLayout>

result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the result activity opened from the notification" >
    </TextView>

</LinearLayout>

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    看看这个

       public static NotificationManager nm;
    public static final int UNIQUE_ID = 123458;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newone);
        createNotification();
    
    }
    public void createNotification() {
        NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final int UNIQUE_ID = 123458;
        Intent navigationIntent = new Intent();
        navigationIntent.setClass(NEWAct.this,
                TestActivity.class);
        navigationIntent.putExtra("selected_tab", "more");
    
        PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
                0);
        String body = "New Notification added!!!";
        String title = "Title";
        Notification n = new Notification(R.drawable.ic_launcher, body,
                System.currentTimeMillis());
        n.number = 2;
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_ALL;
        nm.notify(UNIQUE_ID, n);
    }
    

    在您的活动启动后,您必须使用 id(UNIQUE_ID) 取消该通知

                  try {
            nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nm.cancel(UNIQUE_ID);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    【讨论】:

    • 哦抱歉..response文本是您要查看通知图标的数字,该数字将显示在图标上。唯一ID是我现在正在编辑的常量ID
    • navigationIntent.setClass(CurrentClass.this, DestinationClass.class); 正确吗?
    • 那个notificationreceiver是activity还是Broadcastreceiver?
    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 2013-10-26
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多