繁体   English   中英

我想显示通知,即使我的Android应用正在运行

[英]I want to show notification even if my Android app is running

我正在使用AWS SNS将推送通知发送到SNS主题和设备端点。 如果应用未运行,它将显示通知,但在运行时不显示通知。

即使应用程序正在运行,我也希望显示通知,因为该通知会将应用程序另一部分中发生的更改通知给应用程序用户,以便他们可以在准备就绪时进行检查。 类似于在您向其他人键入消息时从另一个群聊获得新的Whatsapp消息的通知。

以下是通知生成器代码。 任何提示将不胜感激。 谢谢。

    private void displayNotification(final String message) {

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    int requestID = (int) System.currentTimeMillis();

    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Bitmap large_icon = BitmapFactory.decodeResource(this.getResources(),
            R.mipmap.myImage);

    // Display a notification with an icon, message as content, and default sound. It also
    // opens the app when the notification is clicked.

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setLargeIcon(large_icon)
            .setContentTitle("My Subject Title")
            .setContentText(message)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .setContentIntent(contentIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, builder.build());
}

使用以下代码显示通知消息。

 private static void generateNotification(Context context, final String message) 
 {
    try {
        NotificationManager mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
      //Define sound URI
          Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          Intent notificationIntent = new Intent(context, DemoActivity.class);
        notificationIntent.putExtra("message", message);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, RandomNumberGenerator.getRandom(),notificationIntent
                /*new Intent(context, LuncherActivity.class)*/, 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.icon_small);
        mBuilder.setContentTitle("YOUR-APP-NAME");
      //  mBuilder.setStyle(new NotificationCompat.BigTextStyle()
       // mBuilder.bigText(msg);
        mBuilder.setContentText(message);
        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(soundUri);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
        }

  }

创建一个如下所示的随机数生成器类

公共类RandomNumberGenerator {

 public static int getRandom()
 {
    int random_number=0;    
     Random random = new Random();
     random_number= random.nextInt(9999 - 1000) + 1000;
    return random_number;
   }

 }

试试看,让我知道。

我发现了问题。

@Override
public void onMessageReceived(final String from, final Bundle data) {
    String message = getMessage(data);
    Log.d(LOG_TAG, "From: " + from);
    Log.d(LOG_TAG, "Message: " + message);
    // Display a notification in the notification center if the app is in the background.
    // Otherwise, send a local broadcast to the app and let the app handle it.
    displayNotification(message);

  /*  if (isForeground(this)) {
        // broadcast notification
        broadcast(from, data);
    } else {
        displayNotification(message);
    } */
}

我的displayNotification()函数未调用,因为条件条件语句“ if”仅在应用程序不在前台时才显示通知。 因此,现在当onMessageReceived()获取消息时,即使我的应用程序当前正在运行,它也将其直接传递给displayNotification(),这正是我想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM