【问题标题】:Ionic 2 : Local notification iconIonic 2:本地通知图标
【发布时间】:2017-06-26 07:22:20
【问题描述】:

我使用这个插件来获得本地通知: https://github.com/katzer/cordova-plugin-local-notifications/wiki/03.-Installation

我想在我的通知中有一个特定的图标。它位于我的 /www/assets/images/ 文件夹中。

我尝试过这种方式,但它不起作用,我有一个带铃铛的方形图标:

public schedule() {
    cordova.plugins.notification.local.schedule({
      title: "New Message",
      message: "Hi, are you ready? We are waiting.",
      sound: null,
      at: new Date(new Date().getTime() + 5 * 1000),
      icon: 'file://assets/images/logo2.png'
    });
  }

有人可以告诉我我必须写的路径类型吗?我迷路了。

【问题讨论】:

  • 可能是../assets/images/logo2.png
  • 不,它不起作用。 ://

标签: javascript typescript notifications ionic2


【解决方案1】:

对于 ionic 2 插件的本地推送通知,您可以设置如下图标。这里的 icon.png 将从 android 中的 drawable 文件夹中获取。您可以通过在 android 平台部分的配置文件中添加下面提到的行来配置 ionic 以将本地图像文件复制到 drawable 文件夹。

<platform name="android">
<resource-file src="resources/android/icon/icon.png" target="res/drawable/icon.png"/>
</platform>
        

this.localNotifications.schedule({
          id: 1,
          title: data.title,
          text: data.body,
          data: data,
          icon: "res://icon.png",
          smallIcon:"res://icon.png"
        });

【讨论】:

  • 像这样复制,加上图标必须是 72x72 像素,背景透明。
  • 对于较新的 Cordova 版本 (7+?),target 的格式应为 app/src/main/res/drawable/icon.png(与以前不同的基本目录)。
【解决方案2】:

如果您的唯一问题是通知图标在 Android 上正确显示,以下方法对我有用 - 使用 drawable-xhdpi-icon 图标(尺寸 96x96),将其重命名为 icon.png 并将其放置在两个位置:

  • /src/assets/img
  • /platforms/android/res/drawable

drawable文件夹是一个新文件夹,可以通过复制platforms/android/res/mipmap-xhdpi 手动或借助钩子platforms/android/res/drawable。 在您的代码中,localgeofence 通知的引用如下:

smallIcon: 'res://icon',
icon: 'file://assets/img/icon.png'

如果 ionic cordova 资源 是问题的一部分,您可以通过使用最大图标并借助调整大小工具进行一次性设置例如 resizeimage.net,为 iOS 和 Android 创建一组图标。 此处的 Excel https://github.com/dovk/howto_resources-folder 列出了要创建的 .png 文件的大小和名称。然后,您将它们放在各自的资源文件夹中,就像 ionic cordova resources 所做的那样 - 例如在 resources/android/icon、resources/ios /splash 等等。 如果这样做,则不应再使用 ionic cordova platform add androidionic cordova platform add ios ,因为这也是 ionic cordova 资源 - 你需要做的是 cordova 平台添加(没有 ionic 开头)。

【讨论】:

    【解决方案3】:

    我找到了解决办法:

    我在 /platforms/android/res/ 中创建了一个名为“drawable”的新文件夹 我将我的图片放在名为“ic_notifications.png”和“ic_notifications_small.png”的新文件夹中。

    在我写的代码中

    cordova.plugins.notification.local.schedule({
          id: 2,
          title: "Notification",
          message: "Retour à l'application",
          sound: null,
          at: new Date(new Date().getTime() + 5 * 1000),
          icon: 'ic_notifications',
          smallIcon: 'ic_notification_small'
        });
    

    而且它有效!

    【讨论】:

    • 它可以工作,但应用图标的背景会出现灰色。
    • 如何更改本地通知图标的背景颜色
    【解决方案4】:

    在 AndroidManifest.xml 中添加以下元数据即可。

     <application
        android:name="xxxxxx"
            android:label="xxxxxx"
            android:icon="@mipmap/ic_launcher"
            
            >
    
           <meta-data
                    android:name="your_apps_bundle_id.default_notification_icon"
                    android:resource="@drawable/ic_notif" />
    
    ......
    

    【讨论】:

      【解决方案5】:
              var teste = new NotificationImage { FilePath = "@Resources/drawable/IcoApp.png" };
      
      
              var notification = new NotificationRequest
              {
                  Description = "Test Description",
                  Title = "Notification!",
                  ReturningData = "Dummy Data",
                  Image = teste
              };
      

      【讨论】:

      • 应避免使用纯代码答案。即使您的代码确实解决了问题/问题,您也应该添加一些解释。
      • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
      【解决方案6】:

      在不添加图像文件名的情况下提供如下图像路径 LocalNotifications.schedule({ id: 1, title: "Notification Title", text: "Notification Text", icon: 'assets://images/image_name' });

      【讨论】:

        猜你喜欢
        • 2017-01-27
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 2018-05-26
        • 2017-07-28
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        相关资源
        最近更新 更多