簡體   English   中英

應用程序在后台運行時,狀態欄中未收到通知

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

當我在后台進行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM