Push_Notification_guj
//aama sv thi phela firebase jode connect karvu
//pche Cloud Messaging ma jvanu pche New Message pr Click karvu then Message Text ma lkhvu the aapdo pooject
select karvu Spinner pr Click karvu APP lkhelu chu aeni right side pr Spinner hse
Step 1:activity_main.xml And java
Step 2:MyFirebaseInstanceIdService.java
Step 3: MyFirebaseMessagingService
Step 4:Manifest //aama services lkhelu che ie j add karvu
Step 1:activity_main.xml & java
//activity_main.xml and java ma ksho change nai karvano
Step 2;MyFirebaseInstanceIdService.java
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG ="" ;
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String refreshedToken) {
SharedPreferences sp;
sp= getApplicationContext().getSharedPreferences("DeviceToken",MODE_PRIVATE);
SharedPreferences.Editor e=sp.edit();
e.putString("token",refreshedToken);
Log.d("token",refreshedToken);
e.commit();
}
}
Step 3: MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@Override
public void onMessageReceived(RemoteMessage remoteMessage ) {
showNotification(remoteMessage.getData().get("message"),remoteMessage.getData().get("title"));
Log.d("data",remoteMessage.getData().get("message")+remoteMessage.getData().get("title"));
}
private void showNotification(String message , String title) {
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setSound(uri)
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(intent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
Step 4:Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demoproject.aalap.com.webview">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
//pche Cloud Messaging ma jvanu pche New Message pr Click karvu then Message Text ma lkhvu the aapdo pooject
select karvu Spinner pr Click karvu APP lkhelu chu aeni right side pr Spinner hse
Step 1:activity_main.xml And java
Step 2:MyFirebaseInstanceIdService.java
Step 3: MyFirebaseMessagingService
Step 4:Manifest //aama services lkhelu che ie j add karvu
Step 1:activity_main.xml & java
//activity_main.xml and java ma ksho change nai karvano
Step 2;MyFirebaseInstanceIdService.java
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG ="" ;
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String refreshedToken) {
SharedPreferences sp;
sp= getApplicationContext().getSharedPreferences("DeviceToken",MODE_PRIVATE);
SharedPreferences.Editor e=sp.edit();
e.putString("token",refreshedToken);
Log.d("token",refreshedToken);
e.commit();
}
}
Step 3: MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@Override
public void onMessageReceived(RemoteMessage remoteMessage ) {
showNotification(remoteMessage.getData().get("message"),remoteMessage.getData().get("title"));
Log.d("data",remoteMessage.getData().get("message")+remoteMessage.getData().get("title"));
}
private void showNotification(String message , String title) {
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setSound(uri)
.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(intent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
Step 4:Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demoproject.aalap.com.webview">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
Comments
Post a Comment