izac izac - 1 month ago 16
Android Question

broadcastreceiver intent launch activity other application notification

I create 2 app (appA, appB) , from appA i send message to appB, in appB a catch this message and create notification , but i want on i click notification open my appA and see my message.

appA

public class MainActivity extends Activity {
TextView text;
Button send;
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.editText);
send = (Button) findViewById(R.id.button);


View.OnClickListener Onlistbtn = new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(view.getContext(),Main2Activity.class);
intent.putExtra("com.example.abc.send_messege.broadcast.Message",text.getText().toString());
intent.setAction("com.example.abc.send_messege.custom_action");
sendBroadcast(intent);
}
};

send.setOnClickListener(Onlistbtn);
}}


appB

public class MyReceiver extends BroadcastReceiver {
private final static AtomicInteger c = new AtomicInteger(3);
public MyReceiver() {
}


@Override
public void onReceive(Context context, Intent intent) {
String text = intent.getStringExtra("com.example.abc.send_messege.broadcast.Message");
//Intent intent1 = new Intent(context, Main2Activity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.alert_dark_frame)
.setContentTitle("My notification")
.setContentText(text)
.setContentIntent(pIntent)
.setAutoCancel(true);

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(getID(), mBuilder.build());


}
public static int getID() {
return c.incrementAndGet();
}}


AndroidManifest appB

<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.abc.send_messege.custom_action" />
</intent-filter>
</receiver>

Answer

Ok you can try this

We you are creating the PendingIntent pass the new intent to launch activity from another application(appA)

Intent intent = new Intent();

//com.colisa.broadcast is the package of another app
//com.colisa.broadcast.MainActivity is the actity to be launched
intent.setComponent(
   new ComponentName("com.colisa.broadcast","com.colisa.broadcast.MainActivity"));

Then you will pass it to the PengingIntent.setContentIntent(intent)

I haven't triend this but i think you can intent.putExtra(key, message) and on receiving Activity get that messsage via (this post)

if (null != getIntent().getExtras().getString(key)){
  // whatever
}