java - 无法实现通知服务

标签 java android notifications

我引用了以下链接来研究 Android 中通知服务的演示示例:Sai Geetha BlogVogella Tutorial .

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

问题来了,点击该通知时,既没有显示任何消息,也没有触发导航到新 Activity 的 Intent 。

我是这个概念的新手,所以感谢任何帮助...

代码

CreateNotification.class

public class CreateNotification extends Activity {

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

    public void notify(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="notify"
        android:text="Create Notification" >
    </Button>

</LinearLayout>

结果.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>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.notificationmanager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".CreateNotification"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationReceiver" />
    </application>

</manifest>

最佳答案

请尝试修改行:

PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent, 0);

PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

文档:

请注意, Activity 将在现有 Activity 的上下文之外启动,因此您必须在 Intent 中使用 Intent.FLAG_ACTIVITY_NEW_TASK 启动标志。

附在我的代码下方。对我来说,它工作正常。

private void showNotification() {
    Log.i(TAG, "showNotification called...");
    final Intent notificationIntent = new Intent(this, UpdateApplicationActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    final Notification notification = new Notification(R.drawable.htc_notification, getString(R.string.UpdateCheck_update_available), System.currentTimeMillis());
    notification.defaults |= Notification.DEFAULT_ALL;
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this, getString(R.string.UpdateCheck_notification_title), getString(R.string.UpdateCheck_notification_message), contentIntent);

    // Notifying the user about the new update.
    if (notificationManager != null) {
        notificationManager.notify(APP_UPDATE_NOTIFICATION, notification);
    }
}

关于java - 无法实现通知服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10315505/

相关文章:

android - 不支持 Google Play In-app Billing V.3 时

android - EditText 输入速度很慢

java - MYSQL:添加新行值时的通知

firebase - Ionic Cordova 项目中的 Firebase 推送通知问题

java - Java中的FileReader和ready方法

java - 防止jboss 4.2上的XXE攻击

android - 从 Activity 获取 ListView 并以编程方式填充 XML 字符串数组

Swift 使用 NotificationCenter 在 View Controller 之间传递数据

java - 使用java正则表达式模式获取部分匹配字符串

java - 标记和预读限制