如何在android设备屏幕关闭时显示弹出通知
我将尝试以这种格式向我的应用程序发送数据消息如何在android设备屏幕关闭时显示弹出通知,android,json,firebase,push-notification,firebase-notifications,Android,Json,Firebase,Push Notification,Firebase Notifications,我将尝试以这种格式向我的应用程序发送数据消息 { "to": "token", "data": { "title":"Title...", "body":"Body...", "text":"text" } } 然后在这个JSON之后,我使用下面的方法进入我的设备 @Override public void onMessageReceived(Remote
{
"to": "token",
"data": {
"title":"Title...",
"body":"Body...",
"text":"text"
}
}
然后在这个JSON之后,我使用下面的方法进入我的设备
@Override
public void onMessageReceived(RemoteMessage message) {
Map<String, String> map = message.getData();
JSONObject obj = new JSONObject();
for (Object o : map.keySet()) {
String key = o.toString();
String value = map.get(key);
obj.put(key, value);
}
Intent resultIntent = new Intent();
showNotificationMessage(getApplicationContext(), "Test ...", obj.toString(), resultIntent);
}
public void showNotificationMessage(String title,
String message,
Intent intent) {
// Check for empty push message
if (TextUtils.isEmpty(message) && TextUtils.isEmpty(title)) {
return;
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(mContext, 0, intent, 0);
notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
showSmallNotification(title, message, resultPendingIntent, notificationSoundUri);
playNotificationSound();
}
private void showSmallNotification(String title,
String message,
PendingIntent resultPendingIntent,
Uri alarmSound) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(title)
.setShowWhen(true)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(10000), notification);
}
@覆盖
消息接收时的公共无效(RemoteMessage消息){
Map=message.getData();
JSONObject obj=新的JSONObject();
对于(对象o:map.keySet()){
字符串键=o.toString();
字符串值=map.get(键);
对象put(键、值);
}
Intent resultent=新Intent();
showNotificationMessage(getApplicationContext(),“测试…”,obj.toString(),resultent);
}
public void showNotificationMessage(字符串标题,
字符串消息,
意图(意图){
//检查空推送消息
if(TextUtils.isEmpty(消息)和&TextUtils.isEmpty(标题)){
返回;
}
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP | intent.FLAG_ACTIVITY_SINGLE_TOP);
悬而未决的结果悬而未决的结果=
getActivity(mContext,0,intent,0);
notificationSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
showSmallNotification(标题、消息、ResultPendingContent、notificationSoundUri);
播放通知声音();
}
私有无效通知(字符串标题,
字符串消息,
悬而未决的结果悬而未决的结果,
(警报声){
NotificationCompat.Builder mBuilder=新的NotificationCompat.Builder(mContext);
NotificationCompat.InboxStyle InboxStyle=新NotificationCompat.InboxStyle();
inboxStyle.addLine(消息);
通知;
通知=mBuilder.setSmallIcon(R.mipmap.ic_启动器)
.setTicker(标题)
.setShowWhen(真)
.setAutoCancel(真)
.setContentTitle(标题)
.setContentIntent(结果结束内容)
.setSound(警报声)
.setStyle(收件箱样式)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_启动器)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.ic_启动器))
.setContentText(消息)
.build();
NotificationManager NotificationManager=(NotificationManager)mContext.getSystemService(Context.NOTIFICATION\u服务);
notificationManager.notify(新的Random().nextInt(10000),通知);
}
这一次使用这个标题和正文,我想显示通知弹出形式我的设备和我的设备屏幕关闭,这样我就可以在我的设备弹出通知 这是有效的ans,但是在棉花糖下面工作,你对上面的棉花糖有什么建议吗
/Just implement one line/
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
/******************************/
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setTicker(title)
.setShowWhen(true)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND).setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
/**Implement below Method***/
/**And call for "wakeUpLock" Method inside onMessageReceived()**/
private void wakeUpLock()
{
PowerManager pm = (PowerManager)MyGcmListenerService.this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.i(TAG, "screen on: "+ isScreenOn);
if(isScreenOn==false)
{
Log.i(TAG, "screen on if: "+ isScreenOn);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(10000);
}
}