I will try to send data messages to my application like this format.
{
"to": "token",
"data": {
"title":"Title...",
"body":"Body...",
"text":"text"
}
}
Then after this JSON I am getting in my device with below method.
@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);
}
That time using this title and body I want to show notification popup form my device and my device screen off so how I can popup notification in my device.
/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);
}
}