简体   繁体   English

应用程序在后台运行时,状态栏中未收到通知

[英]Not Getting notification in status bar while app running in background

When I receive messages while Google Chat is in the background, I am not getting any notification in the status bar. 当我在后台进行Google聊天时收到消息时,状态栏中没有收到任何通知。

public void showNotification(Context context, String msg, String title,
        String fromChatID) {


    int id = GTalkChatDetails.friendID.indexOf(fromChatID);

    NotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

    int icon = R.drawable.statusicon;
    CharSequence tickerText = msg;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.DEFAULT_SOUND;


    CharSequence contentTitle = title;
    CharSequence contentText = msg;


    Intent notificationIntent = new Intent(context, GTChat.class);
    Bundle bun = new Bundle();
    bun.putString("name", GTalkChatDetails.name);
    bun.putString("chatID", GTalkChatDetails.chatID);
    bun.putString("profileImage", GTalkChatDetails.profileImageURL);
    bun.putString("fromName", title);
    bun.putString("fromChatID", fromChatID);


    notificationIntent.putExtras(bun);



    PendingIntent contentIntent = PendingIntent.getActivity(context, id,
            notificationIntent,PendingIntent.FLAG_ONE_SHOT);

            notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);

    mNotificationManager.notify(id, notification);

    getCurrentActivity(context, msg, title, fromChatID);

}



public void getCurrentActivity(Context context, String msg,
        String fromName, String fromChatID) {


    ActivityManager am = (ActivityManager) context
            .getSystemService(ACTIVITY_SERVICE);

    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;


    if (componentInfo.getClassName().startsWith("com.project")) {

        showNotification(context, msg, fromName, fromChatID);
    } else {
        showNotification(context, msg, fromName, fromChatID);
    }

}
 if (to.equals(fromName)&&notify_checkbox==true) {
  Notification(message.getBody(),fromName, name); 
      }
else
System.out.println("recieved msg" );



  protected void Notification(String body, String fromName, String Notificationmsg) {

    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);      

    String tickerText = getString(R.string.app_name, body);
    Notification notif = new Notification(R.drawable.ic_launcher, tickerText,
            System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, PresentChatActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);


     notif.setLatestEventInfo(PresentChatActivity.this,Notificationmsg, message, pendingIntent);
     notif.setLatestEventInfo(getApplicationContext(), fromName, body,pendingIntent );  
     notificationManager.notify(10001, notif);
     }

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

相关问题 如果应用正在运行,则不会在状态栏中显示通知 - Not show notification in status bar if app is running 状态栏通知单击事件Android的后台前景应用程序状态 - Status bar notification click event Android for background foreground app state Android:在应用程序处于后台时更改状态栏的颜色 - Android: Changing the colour of the status bar while the app is in background 在应用未运行时设置小通知图标背景颜色 - Setting small notification icon background color while app is not running 自定义状态栏通知以进行后台下载 - Custom status bar notification for background download 在Android应用处于前台模式时获取推送通知,但在应用未运行或处于空闲模式时无法获取通知 - Getting push notification while Android app is in foreground mode but unable to get notification while app is not running or idle mode 当应用程序未运行时,不在状态栏上显示推送通知(Firebase 云消息) - Not show push notification (Firebase Cloud Message) on status bar when app is not running 当应用程序不在 Android 上运行时,使用 React Native 在状态栏上显示通知图标 - Show notification icon on status bar with React Native when app is not running on Android Cordova应用程序状态栏图像通知 - Cordova app status bar image notification Phonegap中的通知当应用程序未运行时 - Notification in Phonegap While App is not running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM