Where the mind is without fear..
var message = ‘Good Morning’;
var payload = {
notification: {
title: 'Notification from Node js Server',
body: message,
'click_action': 'OPEN_ACTIVITY_1'
},
data: {
'extra': message
}
};
<activity
android:name=".NotificationView"
android:label="@string/title_activity_notification_view"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="OPEN_ACTIVITY_1"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
public class NotificationView extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
setTitle("Good Morning");
Intent intent = getIntent();
if(intent.getExtras() != null){
Bundle bundle = intent.getExtras();
if(bundle != null){
String value = bundle.getString("extra");
if(value != null){
TextView tv = findViewById(R.id.notification_text);
tv.setText(value);
}
}
}
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage){
if(remoteMessage.getNotification() != null){
try {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
addNotification(body);
}catch(Exception e){
e.printStackTrace();
}
}
}
private void addNotification(String topic){
createNotificanChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("Notification from ReceiverService")
.setContentText(topic)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setChannelId(MainActivity.CHANNEL_ID);
Intent notificationIntent = new Intent(this, NotificationView.class);
notificationIntent.putExtra("extra",topic);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
