【问题标题】:Notification displays numbers, not the contentText通知显示数字,而不是 contentText
【发布时间】:2021-01-29 14:48:50
【问题描述】:

我的通知显示数字,而不是文本。我的字符串是西里尔字母,但这应该不是问题。另外,字符串的长度是否重要? ContentText 包含 5 个简短的单词。

这里是通知的sn-p代码,有效,但只显示数字:

NotificationCompat.Builder builder = new NotificationCompat.Builder( this, CHANNEL_ID) ;
    builder.setContentTitle(String.valueOf(R.string.appName));
    builder.setContentIntent(notifyPendingIntent);
    builder.setContentText(String.valueOf(R.string.wRevNow));
    builder.setSmallIcon(R.drawable.logo ) ;
    builder.setAutoCancel(true);
    builder.setChannelId(CHANNEL_ID) ;
    return builder.build();

这是我得到的:

【问题讨论】:

    标签: java android push-notification


    【解决方案1】:

    问题在于String.valueOf(R.string.appName),其中R.string.appName 是标识特定资源的唯一整数,String.valueOfinteger value 转换为string,从而使您的通知中出现数字滑动,请尝试以下操作

    // code remains same
    
    builder.setContentTitle(getString(R.string.appName));
    
    builder.setContentText(getString(R.string.wRevNow));
    
    //rest of the logic remains same
    

    【讨论】:

    • 当然,如果解决了,请接受答案,祝您编码愉快:)
    【解决方案2】:

    R.string 资源包包含 int 值,它们代表应用程序资源中的字符串。 String.valueOf() 仅返回该 int (id) 的字符串表示形式。从上下文中使用 getString(id: Int) 方法来访问它们。您的代码如下所示:

    NotificationCompat.Builder builder = new NotificationCompat.Builder( this, CHANNEL_ID) ;
        builder.setContentTitle(getString(R.string.appName));
        builder.setContentIntent(notifyPendingIntent);
        builder.setContentText(getString(R.string.wRevNow));
        builder.setSmallIcon(R.drawable.logo ) ;
        builder.setAutoCancel(true);
        builder.setChannelId(CHANNEL_ID) ;
        return builder.build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2015-08-28
      相关资源
      最近更新 更多