当我从通知中启动它时,我的某个应用程序出现问题.它从未出现在"最近的应用"列表中.
没有通知,每件事都按预期工作:我启动应用程序,导航进入,当我退出它时(使用主页按钮或后退按钮)之后,我可以通过长按主页按钮=>确定返回到它.
当我收到通知时,问题就开始了.如果我从通知启动应用程序,它会启动正确的屏幕,我可以使用app => ok.但是当我退出应用程序(使用主页或后退按钮)时,它不再出现在"最近的应用程序"列表中.更确切地说:
下面是我在状态栏中添加通知的原始代码:
mNotification = new Notification(R.drawable.ic_notif, message, System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.putExtra(...);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), message, contentIntent);
notificationManager.notify(ConfigApp.NOTIFICATION_ID, mNotification);
Run Code Online (Sandbox Code Playgroud)
我尝试添加FLAG_ACTIVITY_NEW_TASK但它没有帮助:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(link));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra(...);
Run Code Online (Sandbox Code Playgroud)
通知启动的活动的清单声明:
<activity
android:name=".activity.ActivityMessages"
android:label=""
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="hostapp"
android:pathPrefix="/prefixapp"
android:scheme="schemeapp" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
有人知道如何在通知启动后将应用程序保留在"最近的应用程序"列表中吗?
我想在我的应用程序中添加一个Notification操作项,这是一个音乐播放器.启动流时,应触发通知,并在通知中显示流的停止按钮.到目前为止通知工作正常,我遇到了停止操作项目的问题.以下是在启动流的服务中声明它的方式:
Intent stopIntent = new Intent(this, MusicPlayerNew.class);
stopIntent.putExtra("STOP", "STOP");
PendingIntent stopPendingIntent = PendingIntent.getActivity(this, 0,
stopIntent, PendingIntent.FLAG_UPDATE_CURRENT, null);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent);
Run Code Online (Sandbox Code Playgroud)
现在在onResume() - 我的活动方法中,我用getIntent().getStringExtra()检查"STOP"额外,但我通过getIntent()检索的意图没有设置额外的东西:(
我还试图检查发送广播(我有一个广播接收器工作从服务到活动进行通信)
Intent stopIntent2 = new Intent(MusicPlayerNew.STOP_MEDIAPLAYER);
PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
stopIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_stat_stop, "Stop", stopPendingIntent2);
Run Code Online (Sandbox Code Playgroud)
现在,如果活动当前位于前台,则此功能正常.如果活动在后台,则停止按钮不执行任何操作:(
编辑: 我在我的Activity中将BroadcastReceiver作为私有类
private class DataUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
..
}}
Run Code Online (Sandbox Code Playgroud)
在onResume()中注册我的app接收器:
intentFilter = new IntentFilter(STOP_MEDIAPLAYER);
registerReceiver(dataUpdateReceiver, intentFilter);
Run Code Online (Sandbox Code Playgroud)
的onPause()
unregisterReceiver(dataUpdateReceiver);
Run Code Online (Sandbox Code Playgroud)
现在,如果我从onPause() - 方法中删除取消注册,即使app/activity不在前台,也会收到广播.但这是正确的方法吗?我从网上的教程中得到了这个注册/取消注册的内容我认为..
android android-intent android-notifications android-activity android-pendingintent
我正在尝试编写一个使用的演示setVisibility()来控制Android 5.0锁屏上显示的内容Notification.但是,似乎没有效果:
默认设置VISIBILITY_PRIVATE仍然显示私有Notification,而不是公共对应
VISIBILITY_SECRET 通知仍会显示在锁定屏幕上
IOW,一切都表现得好像VISIBILITY_PUBLIC有效,至少当我测试运行Android 5.0图像的Nexus 7时,我们在Android 5.0发布后不久就给出了(构建LPX13D).所以我不知道问题是否与我的代码,此设备或Android中的错误有关.
我有两个版本的相同示例应用程序:
一个使用NotificationCompat和NotificationManagerCompat
其他的用途Notification和NotificationManager使用minSdkVersion的21和targetSdkVersion21
(请注意,这些项目主要用于Android Studio; Eclipse用户可以导入项目,但可能需要进行少量修复,特别是对于support-v13第一个示例的库引用)
样本用于AlarmManager触发Notification工作,主要是因为您有机会返回锁定屏幕以查看结果.这是BroadcastReceiver由AlarmManager(显示NotificationCompat版本)触发的:
/***
Copyright (c) 2014 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may …Run Code Online (Sandbox Code Playgroud) 当我将代码添加mNotificationBuilder.setLargeIcon(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_large_icon));到我的通知中时,它会停止工作而不会出现错误或警告.这只发生在棒棒糖前,棒棒糖上,超出它的效果很好.而"作品"我的意思是通知出现了.
我的示例代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.icon);
mBuilder.setContentTitle("Content Title");
mBuilder.setContentText("Content Text");
mBuilder.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.drawable.ic_large_icon));
startForeground(1, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
我试图以不同的方式加载位图,但它仍然失败...图标是128x128所以它的大小不应该是一个问题?
我也试过不同的id但没有解决问题.
我会非常感谢任何建议,请向正确的方向努力,对我来说意味着世界.
编辑1#
此通知是从服务发出的.服务是活动的,Log打印告诉我"startForeground()"之后的代码运行.
我在App上工作,我需要在其中显示通知.对于通知,我正在使用FireBase云消息传递(FCM).当app在后台时,我能够获得通知.
但是当我点击通知时,它会重定向到home.java页面.我希望它重定向到Notification.java页面.
所以,请告诉我如何在点击通知中指定活动.我使用两种服务:
1.)MyFirebaseMessagingService
2.)MyFirebaseInstanceIDService
这是我在MyFirebaseMessagingService类中的onMessageReceived()方法的代码示例.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMessageService";
Bitmap bitmap;
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " …Run Code Online (Sandbox Code Playgroud) android android-notifications firebase-cloud-messaging firebase-notifications
目前,我正在开发类似"待办事项列表"的应用程序.我已在我的应用程序中成功实现了NotificationService和SchedularService.此外,我在为任务设置的时间收到警报(通知).以下是我的疑问:
Notification notification = new Notification(icon, text, time);和notification.setLatestEventInfo(this, title, text, contentIntent);.在developer.android.com上,他们建议使用Notification.Builder.那么如何使我的应用程序与所有API级别兼容.这是用于安排警报的代码段:
...
scheduleClient.setAlarmForNotification(c, tmp_task_id);
...
Run Code Online (Sandbox Code Playgroud)
这是ScheduleClient.java类:
public class ScheduleClient {
private ScheduleService mBoundService;
private Context mContext;
private boolean mIsBound;
public ScheduleClient(Context context)
{
mContext = context;
}
public void doBindService()
{
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public …Run Code Online (Sandbox Code Playgroud) I've been playing around with Android Wear and making some dummy apps. What I've noticed is that Android Wear doesn't seem to show the notifications that are ongoing,i.e., notifications with setOngoing(true).
Any way to show these notifications on Wear???
我自己的应用程序使用与2016年Google I/O应用程序所示的完全相同的技术.请参阅源代码
我需要在非常具体的时间点提醒用户 - 使用通知.
为此,我使用它AlarmManager在正确的时间点唤醒设备:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)
这pendingIntent是这样创建的:
final Intent intent = new Intent(MyAlarmService.ACTION_NOTIFY_RIDE, null, this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
现在,我的MyAlarmService类是一个简单的IntentService处理唤醒只是为了为用户创建通知.
我在日志中收到的消息如下:
W/ActivityManager: Background start not allowed: service Intent { act=xxx.xxx.xxx.action.NOTIFY_RIDE flg=0x4 cmp=xxx.xxx.xxx./xxx.xxx.xxx.service.MyAlarmService (has extras) }
Run Code Online (Sandbox Code Playgroud)
现在,谷歌自己的实施显然已被打破 - 即使我不想做任何繁重的背景工作,我也不能再使用这种技术了.但是,我应该如何在非常具体的时间点唤醒用户呢?(想想我的应用程序作为闹钟)
Notification在Android O中发送时,我必须指定NotificationChannel要发送到.
如果我使用这样的旧方法(不设置任何通道)NotificationCompat.Builder(this),Notification则不会显示.
这同样适用于这样一个无效的信道NotificationCompat.Builder(this, "invalid")或NotificationCompat.Builder(this, "").
当我通过Firebase Cloud Messaging发送通知并且我的应用程序在后台没有指定通知通道时,它将是"其他"通道中的通知.
当我尝试在上面提到的前景中做同样的事情时,这将不起作用,也不会创建名为"Miscellaneous"和id "{package} .MISCELLANEOUS"的通知通道,然后通过它发送.当我这样做时会发生以下情况:
如何在没有像FCM这样的频道的情况下发送通知,那么它是否会进入常规的"杂项"频道?
正如我上面提到的,它发生在FCM通知中,但例如Gmail也使用杂项频道.那我该怎么用呢?
我相信如果它通常无法使用,他们会删除杂项频道.
为什么此代码未向"其他"通知渠道发送通知,实际上并未发送任何通知(仅在Android O上,代码适用于较低的Android版本).
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(1, NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.small_icon)
.setContentTitle(URLDecoder.decode("Title", "UTF-8"))
.setContentText(URLDecoder.decode("Text", "UTF-8"))
.setColor(ContextCompat.getColor(applicationContext, R.color.color))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT))
.build())
Run Code Online (Sandbox Code Playgroud) TLDR:通过FCM发送到Android设备的消息需要10秒到5分钟才能发送.可能是由于优先权.我把它设置为"高",但似乎它不会保持这个价值.
我为iOS和Android开发了一个应用程序.我的后端运行在Django上.对于我们的实时通信,我们最近开始使用Firebase云消息传递(FCM).
我成功地设法连接Django服务器,并能够向这两种设备发送消息.
这是我构建消息的python代码.请注意,令牌数据会在以后动态添加,并且消息是静默通知.
def _build_silent_message(not_id, data):
"""Construct silent notifiation message.
Silent means that this message won't show up in the notifications hub
of the app.
"""
return {
'message': {
'data': {"data": data},
'apns': {
'payload': {
"notId": not_id, # notId HAS TO BE FIRST!!!
'aps': {
'content-available': 1
}
},
'headers': {
'apns-priority': '10'
},
},
'android': {
'priority': "high",
'data': {
"androidData": data,
'content-available': '1'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我将Apple和Android优先级设置为"高"(/ 10).在iOS上,所有消息都会立即传递.然而,在Android上并非如此.消息最多需要5分钟才能到达手机.我认为这可能是一个优先问题,因为文件说明:
高度优先.FCM尝试立即传递高优先级消息,允许FCM服务在必要时唤醒休眠设备并运行一些有限的处理(包括非常有限的网络访问).
到现在为止还挺好.我将优先级设置为高.但文件进一步指出:
高优先级消息通常会导致用户与您的应用互动.如果FCM检测到他们没有的模式,则可能会对您的消息进行去优先级排序.
我发送到设备的所有消息都需要与手机进行交互.但我所有的消息都是无声的消息.也许FCM认为我的消息不需要用户交互,因此优先级较低. …
android android-notifications firebase firebase-cloud-messaging