简体   繁体   English

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

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

I am using AWS SNS to send push notification to both SNS topics and Device Endpoints. 我正在使用AWS SNS将推送通知发送到SNS主题和设备端点。 It shows notification if the app is not running but does not show notification while the app is running. 如果应用未运行,它将显示通知,但在运行时不显示通知。

I will like to show notification even if the app is running because the notification will inform the app user of changes happening in another section of the app so they can go check it out when they are ready. 即使应用程序正在运行,我也希望显示通知,因为该通知会将应用程序另一部分中发生的更改通知给应用程序用户,以便他们可以在准备就绪时进行检查。 Something like getting notification of a new Whatsapp message from another group chat while you are typing message to another person. 类似于在您向其他人键入消息时从另一个群聊获得新的Whatsapp消息的通知。

Below is the notification builder code. 以下是通知生成器代码。 Any hint will be really appreciated. 任何提示将不胜感激。 Thanks. 谢谢。

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

Use this below code to show notification message. 使用以下代码显示通知消息。

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

  }

Create a random number genetor class like below 创建一个如下所示的随机数生成器类

public class RandomNumberGenerator { 公共类RandomNumberGenerator {

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

 }

Try it and let me know.its working in my case. 试试看,让我知道。

I found the problem. 我发现了问题。

@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);
    } */
}

my displayNotification() function was not called because of the "if" conditional statement that will only display notification if the application is not in foreground. 我的displayNotification()函数未调用,因为条件条件语句“ if”仅在应用程序不在前台时才显示通知。 So now when onMessageReceived() gets the message it passes it directly to displayNotification() even if my application is currently running, which is what I want. 因此,现在当onMessageReceived()获取消息时,即使我的应用程序当前正在运行,它也将其直接传递给displayNotification(),这正是我想要的。

暂无
暂无

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

相关问题 我想在我的应用程序图标上显示未读通知计数 - I want to show unread notification count on my app icon 如何显示通知甚至应用程序未运行 - How to show a notification even app is not running 我想在我的Android应用中使用“解析通知” - I Want to Use “Parse Notification” in my Android App 如何安排3个事件通过android中的(通知栏)通知用户,即使我的应用未运行,该事件也会运行 - How to schedule 3 events to notify user via (notification bar) in android which runs even when my app is not running 我想在我的Android应用程序中显示Reginonal语言(Gurumukhi) - I want to show the Reginonal language(Gurumukhi) in my Android app 当应用程序处于后台或被杀死时,我想在 Android 中显示自定义 firebase 推送通知 state - I want to show a customize firebase push notification in Android when app is in background or killed state 我想从我的PHP应用程序向我的android应用程序发送通知 - I want to send a notification from my PHP application to my android app 我希望我的应用程序在(线程或作为服务)中运行,即使 onDestroy() 被调用(用户从后台删除) - I want my app to be running in a (Thread or as a service) even when onDestroy() is called (user removed from background) 即使应用未在Android中运行,如何按时间或间隔发送通知? - How to send notification by timing or interval even the app is not running in Android? Android Workmanager,即使应用程序已经在前台,也会加快工作显示通知 - Android Workmanager, expedited work show notification even if app is already in foreground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM