Tuesday, November 21, 2017
Sunday, November 19, 2017
android show notification like viber when app is in background
Manifest.xml
------------------------------------------------
<activity android:name=".pushnotification.NewMsgDialogActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
MyFirebaseMEssagingService.java
------------------------------------------------
final Intent intent = new Intent(MyFirebaseMEssagingService.this, NewMsgDialogActivity.class);
intent.putExtra("trainerId", trainer_id);
intent.putExtra("trainer_image_str", trainer_image_str);
intent.putExtra("trainer_name", trainer_name);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MyFirebaseMEssagingService.this, 0, intent, 0);
try {
// Perform the operation associated with our pendingIntent
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
NewMsgDialogActivity.java
------------------------------------------------
public class NewMsgDialogActivity extends Activity {
String trainerId, trainer_name, trainer_image_str = "";
public static String new_msg = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
show_new_msg_dialog();
}
public void show_new_msg_dialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(getResources().getString(R.string.new_msg))
.setCancelable(false)
.setIcon(R.drawable.messages_icon)
.setMessage(NewMsgDialogActivity.new_msg)
.setNegativeButton("Ignore", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
dialoginterface.dismiss();
finish();
}
})
.setPositiveButton("Check", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
goto_voice_msg_activity();
dialoginterface.dismiss();
finish();
}
}).show();
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
android check app is running foreground
static boolean shouldShowNotification(Context context) {
ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
if (myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
return true;
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
// app is in foreground, but if screen is locked show notification anyway
return km.inKeyguardRestrictedInputMode();
}
ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
if (myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
return true;
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
// app is in foreground, but if screen is locked show notification anyway
return km.inKeyguardRestrictedInputMode();
}
Subscribe to:
Posts (Atom)