我正在使用我的应用测试推送通知。
当应用程序位于前台时:
for i in range(pin_num):
prompt_user()
weight(pin)
def weight(value):
int_values = [] # Create an empty list to store the integers
for char in value:
int_values.append(int(char)) # Converts char to int and adds to list.
#Calculates the weight after the loop has finished
weight=abs(int_values[0] - int_values[1]) + abs(int_values[1]
- int_values[2])+ abs(int_values[2]
- int_values[3])+abs(int_values[3] - int_values[4])+abs(int_values[4]
- int_values[5])+abs(int_values[5] - int_values[6])+abs(int_values[6]
- int_values[7])+abs(int_values[7] - int_values[8])
#pin_weight = weight(pin)
print('The PIN {} has a weight of {}'.format(value,weight))
return
当应用在后台/被杀死时:(效果很好)
Step 1. Received the notification (in system tray).
2. now, I'm in some other screen than the home screen.
3. Actual Problem: On tap on the notification, it is going to the home screen.
4. Expected: If the app is in the foreground, just I need to cancel on tap of the notification. (No need to swipe.)
尝试以意图设置启动模式标志。没有帮助。下面是我的代码。请建议解决方案的人。
Step 1. Received the notification (in the system tray)
2. On tap, open the home screen of the app.
答案 0 :(得分:1)
您可以使用“关闭”按钮创建“自定义通知”,以使用RemoteViews关闭通知
//使用RemoteViews创建通知
RemoteViews remoteViews= new RemoteViews(getApplicationContext().getPackageName(), R.layout.your_custom_notification);
Intent closeIntent = new Intent(context, CloseNotificationService.class);
hangUpIntent.setAction("close");
PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, closeNotification, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.cancel_notification, pendingCloseIntent);
// create notification here..
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteViews)
.build();
关闭按钮的onClick,它将重定向到服务类别:
public class CloseNotificationService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public CloseNotificationService() {
super("notificationIntentService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
switch (intent.getAction()) {
case "close":
Handler hangUpHandler = new Handler(Looper.getMainLooper());
hangUpHandler.post(new Runnable() {
@Override
public void run() {
NotificationManager notifManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancel(notificationId); // get notification id from intent.
}
});
break;
}
}
}
有关RemoteViews的更多信息,您可以参考Google开发者官方网站https://developer.android.com/training/notify-user/custom-notification
答案 1 :(得分:0)
在启动器活动中,您是否尝试过通知管理器类cancelAll()方法? 这样,如果已经有启动通知,它将自动取消
答案 2 :(得分:0)
不确定是否要在点击时关闭通知,但由于您担心的是导航错误。
如果应用程序处于前台状态,我们可以检查应用程序是否处于前台状态,并阻止通过通知单击打开新活动。
//If app is in foreground setting pending intent to null
PendingIntent pendingIntent;
Intent notificationIntent = new Intent(getApplicationContext(), Main2Activity.class);
if(isAppInForeground()){
Log.e("--^","inForeground");
pendingIntent = null;
}else{
Log.e("--^","inBackground");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
添加此功能(来源:link)
private boolean isAppInForeground() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();
boolean isActivityFound = false;
if (services.get(0).processName
.equalsIgnoreCase(getPackageName()) && services.get(0).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
isActivityFound = true;
}
return isActivityFound;
}
在这种情况下,如果在应用程序处于前台时发出通知,则单击该按钮将不执行任何操作。因此,用户只有一个选项可滑动以将其删除。
答案 3 :(得分:0)
代替此:
Intent resultIntent = new Intent(this, MainActivity.class);
执行此操作:
Intent resultIntent = getPackageManager().getLaunchIntentForPackage("your.package.name");
,然后将其放入您的Notification
。如果尚未运行该应用程序,则将启动该应用程序;否则,它将以用户上次使用该应用程序时的状态将应用程序的任务置于前台。如果用户已经在应用程序中(即,在另一个屏幕上),则当用户单击Notification
时,此操作将无效。
应该正是您想要的。