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

时间:2016-07-21 03:15:42

标签: android amazon-web-services push-notification notifications amazon-sns

我正在使用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());
}

2 个答案:

答案 0 :(得分:0)

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

IsEngagedwithWork
  

创建一个随机数的genetor类,如下所示

public class RandomNumberGenerator {

 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();
    }
        }

  }

尝试并让我知道。在我的情况下工作。

答案 1 :(得分:0)

我发现了问题。

@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(),这就是我想要的。