【问题标题】:What is the alternative for the deprecated Notification class in the image?图像中已弃用的 Notification 类的替代方法是什么?
【发布时间】:2014-02-18 11:14:20
【问题描述】:

我打算在 android 设备的状态栏通知中显示我的通知。 我正在关注 1 年前的本教程。 http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/#comment-28784 .它实际上工作正常,但java代码看起来不太好。即使它工作正常,也应该对其进行修改。

在这种情况下,通知类的替代类是什么?如何修改我的代码?

【问题讨论】:

    标签: android-intent push-notification google-cloud-messaging cordova-plugins


    【解决方案1】:

    你可以这样做

    NotificationManager mNotificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    Notification notification = null;
    
    Intent intent = new Intent(context, PdfDialogAlert.class);
    intent.putExtra("dialog", 1);
    PendingIntent piDismiss =
        PendingIntent.getActivity(context, 0, intent, 0);
    
    builder.setContentIntent(piDismiss)
        .setSmallIcon(R.drawable.ic_notif)
        .setWhen(System.currentTimeMillis())
        .setContentTitle("fileName")
        .setTicker(message)
        .setContentText("message");
    
    notification = builder.build();
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    

    【讨论】:

    • 非常感谢您的精彩回答。现在我只有一个问题,即如何在单击状态栏中删除通知?我实际上添加了这段代码“mBuilder.setAutoCancel(true);”但不起作用。
    • 在上面的代码中 FLAG_AUTO_CANCEL 会这样做看到这个developer.android.com/reference/android/app/…
    • 嗨,我的代码中有一个错误。每当我们创建一个新的用户帐户时,我们应该始终从 Eclipse 运行我们的应用程序两次,以便能够显示通知。你对这个问题有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2021-12-13
    相关资源
    最近更新 更多