Skip to content

Instantly share code, notes, and snippets.

@xrd
Created November 15, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xrd/4c2dbb6c8bef472bd55ec177678ed83f to your computer and use it in GitHub Desktop.
Save xrd/4c2dbb6c8bef472bd55ec177678ed83f to your computer and use it in GitHub Desktop.
package com.example.android.basicnotifications;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.view.View;
/**
* The entry point to the BasicNotification sample.
*/
public class MainActivity extends Activity {
/**
* A numeric value that identifies the notification that we'll be sending.
* This value needs to be unique within this app, but it doesn't need to be
* unique system-wide.
*/
public static final int NOTIFICATION_ID = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_layout);
}
/**
* Send a sample notification using the NotificationCompat API.
*/
public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
public void sendNotification(View view) {
String replyLabel = getResources().getString( R.string.reply_label );
String []replyChoices = getResources().getStringArray(R.array.reply_choices);
RemoteInput remoteInput = new RemoteInput.Builder( EXTRA_VOICE_REPLY )
.setLabel( replyLabel )
.setChoices( replyChoices )
.build();
Intent replyIntent = new Intent( this, ReplyActivity.class );
PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT );
NotificationCompat.Action action = new NotificationCompat.Action.Builder( android.R.drawable.ic_dialog_email,
getString( R.string.label ), replyPendingIntent )
.addRemoteInput(remoteInput)
.build();
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.ic_input_add)
// .setLargeIcon(android.R.drawable.ic_dialog_dialer)
.setContentTitle( "The title of the notification")
.setContentText( "The text for the notification")
.extend( new NotificationCompat.WearableExtender().addAction( action ))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
int notificationId = 1000;
notificationManager.notify( notificationId, notification );
}
}
package com.example.android.basicnotifications;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.RemoteInput;
import android.util.Log;
/**
* Created by cdawson on 11/14/16.
*/
public class ReplyActivity extends Activity{
private CharSequence getMessageText( Intent intent ) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
return remoteInput.getCharSequence(MainActivity.EXTRA_VOICE_REPLY);
}
else {
return "";
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_layout);
Intent intent = getIntent();
CharSequence cs = getMessageText( intent );
Log.v( "ReplyActivity", cs.toString() );
// CharSequence cs = getMessageText(savedInstanceState);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment