点击通知 - 发送到应用程序 [英] Click Notification - Send to application

查看:125
本文介绍了点击通知 - 发送到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我的通知系统,当发送的通知,我希望能够点击通知,并把它送我去我的申请。不过,我目前的code,它不这样做。我该如何解决呢?

 公共无效causeNotification(CharSequence的contentText,为CharSequence tickerText){
    Log.d(TAG发送通知 - \\+ contentText +\\);
    字符串NS = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);
    INT图标= R.drawable.neoseeker_swoosh;
    时长= System.currentTimeMillis的();    通知通知=新的通知(图标,tickerText时);
    notification.defaults | = Notification.DEFAULT_ALL;
    notification.defaults | = Notification.FLAG_AUTO_CANCEL;    上下文的背景下= getApplicationContext();
    CharSequence的contentTitle =Neoseeker;
    意图notificationIntent =新的Intent();
    的PendingIntent contentIntent = PendingIntent.getActivity(Neoseeker.this,0,notificationIntent,0);    notification.setLatestEventInfo(背景下,contentTitle,contentText,contentIntent);    mNotificationManager.notify(Neoseeker.NOTIFICATION_ID,通知);
}


解决方案

第1步:一定不要调用 getApplicationContext() Neoseeker.this 显然是一个上下文 - 使用

第二步: notificationIntent 是一个空的意图。这是不会打开任何活动。您需要创建一个意图将开始活动,就像任何意图你会用<$ C $使用C> startActivity()。

下面是一个示例项目我书籍,显示使用的通知。

With my notification system, when the notification is sent, I would like to be able to click on the notification and have it send me to my application. But, with my current code, it doesn't do so. How can I fix that?

public void causeNotification(CharSequence contentText, CharSequence tickerText) {
    Log.d(TAG, "Sending notification - \"" + contentText + "\"");
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.neoseeker_swoosh;
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    notification.defaults |= Notification.DEFAULT_ALL;
    notification.defaults |= Notification.FLAG_AUTO_CANCEL;

    Context context = getApplicationContext();
    CharSequence contentTitle = "Neoseeker";
    Intent notificationIntent = new Intent();
    PendingIntent contentIntent = PendingIntent.getActivity(Neoseeker.this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    mNotificationManager.notify(Neoseeker.NOTIFICATION_ID, notification);
}

解决方案

Step #1: Never call getApplicationContext(). Neoseeker.this apparently is a Context -- use it.

Step #2: notificationIntent is an empty Intent. That is not going to open any activities. You need to create an Intent that would start an activity, much like any Intent you would use with startActivity().

Here is a sample project from one of my books, showing the use of notifications.

这篇关于点击通知 - 发送到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆