Skip to content

Instantly share code, notes, and snippets.

@ankitdubey021
Last active June 25, 2019 11:43
Embed
What would you like to do?
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioAttributes;
import android.util.Log;
import com.ebiztechnocrats.biofuelindia.OldQuotation;
import com.ebiztechnocrats.biofuelindia.R;
import com.ebiztechnocrats.biofuelindia.ViewResponses;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import static com.ebiztechnocrats.biofuelindia.BuyerPlants.TAG;
public class MyFirebaseService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.e("My Token",token);
}
});
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
int type=getSharedPreferences("login_info",MODE_PRIVATE).getInt("usertype",-1);
Map<String, String> data = remoteMessage.getData();
String body = data.get("body");
String title = data.get("title");
Intent intent;
if(type==2){
intent = new Intent(getApplicationContext(), ViewResponses.class);
}
else
intent = new Intent(getApplicationContext(), OldQuotation.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 101, intent, 0);
NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel = new NotificationChannel("222", "my_channel", NotificationManager.IMPORTANCE_HIGH);
nm.createNotificationChannel(channel);
}
NotificationCompat.Builder builder =
new NotificationCompat.Builder(
getApplicationContext(), "222")
.setContentTitle(title)
.setAutoCancel(true)
.setLargeIcon(((BitmapDrawable)getDrawable(R.drawable.logo)).getBitmap())
.setSmallIcon(R.drawable.logo)
//.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.electro))
.setContentText(body)
.setSmallIcon(R.drawable.logo)
.setContentIntent(pi)
;
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
nm.notify(101, builder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment